Skip to content Skip to sidebar Skip to footer

Display Back Button Of Action Bar Is Not Going Back In Android

I am developing an Android app. I am using ActionBar with AppCompactActivity. In my app, I add back button to action bar. But when I click on it, it is not going back to the previo

Solution 1:

Add the following to your activity.You have to handle the click event of the back button.

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()){
         case android.R.id.home:
              onBackPressed();
              returntrue;
       }
   returnsuper.onOptionsItemSelected(item);
 }

Solution 2:

Here you have 2 options:

a) provide a parentActivityName to your SecondActivity tag in AndroidManifest.xml like this:

<activity...android:name=".SecondActivity"android:parentActivityName=".MainActivity" >

b) override onOptionsItemSelected in SecondActivity like this:

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home buttoncase android.R.id.home:
        onBackPressed();
        returntrue;
    }
    returnsuper.onOptionsItemSelected(item);
}

I would suggest reading this guide for more information.

Solution 3:

I would suggest not to handle "android.R.id.home" in onOptionsItemSelected as it is brittle. Rather you should override onSupportNavigateUp method.

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()){
         case android.R.id.home:
              onBackPressed();
              returntrue;
       }
   returnsuper.onOptionsItemSelected(item);
 }

Note: If you are using onOptionsItemSelected , then you should return false as default otherwise onSupportNavigateUp method is not called.

Solution 4:

Here is your code

publicclassEditProfileActivityextendsAppCompatActivity {

        @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.edit_profile);
            Toolbartoolbar= (Toolbar)findViewById(R.id.profile_action_toolbar);
            setSupportActionBar(toolbar);
            setTitle("Edit Profile");
            ActionBar actionBar= getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        @OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
            intid= item.getItemId();
            if (item.getItemId() == android.R.id.home) {
                   finish();
            }

            returnsuper.onOptionsItemSelected(item);
        }
    }     

Solution 5:

Add this to your Activity, in onCreate()

Toolbartoolbar= findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    toolbar.setNavigationOnClickListener(v -> {
        //What to do when back is clicked
        finish();
    });

Post a Comment for "Display Back Button Of Action Bar Is Not Going Back In Android"