display back button on action bar/ toolbar

Brief Introduction:
In this tutorial we are gonna go through some of the ways which can be used to show/display back button in the action bar or in another term toolbar. This can be achieved in many ways and we are gonna discuss them here


Last updated: The post last updated on June 17

Change Note: If I found a new ways to do the same job or improvement then that will be added as Edit: in bold. The Edit will also have a number indicating how many time this has been edited.

Requirements:
The below image is the sample of what we are going to implement


Working - How to achieve/implement:
The best and easyest way of doing this is to override `onSupportNavigateUp()` method. Here is how to get the desired behaviour with this method.
Step 1: For showing the back button add the below line in `onCreate()` method
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //show back button

Step 2 A: override method
       
@Override
public boolean onSupportNavigateUp(){  
    finish();  
    return true;
}
       
 

That's it. I will list the benefits of this approach at the end.
The above method is applicable if you're using `AppCompatActivity` which is the default in android studio. If you are not using `AppCompatActivity` then you have to use the above code without the word Support i.e.
       
getActionBar().setDisplayHomeAsUpEnabled(true);   //show back button

@Override public boolean onNavigateUp(){ finish(); return true; }

Step 2 B: add meta-data to the activity in manifest file
       
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
       
 

Step 2 C: override the `onOptionsItemSelected`
       
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
       
 

You can also simplify the above code Step 2 C as if you don't have other option menu
       
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    onBackPressed();
    return true;
}
       
 


Conclusion:
You can use any of the given methods to achieve the back button implementation but `onSupportNavigateUp()` has some benefits over other methods that are as below.
1. It can be usefull when you want to pass some information to the parent activity.
2. It will not go to home tab if you are implementing tab navigation. Using this method you will be on the same tab when you open the child activity.
3. It will not clear data in your parent activity i.e. if you have some editText and have some value in it that will be unchanged.
4. It's more meaningful as its specifically made for this scenario.

Recommended for you:
=> FreeBitcoin.win earn $0.0018 of 1 minute survey (unlimited survey) & 5000+ satoshi from shortlinks
=> FireFaucet the best auto faucet, multiple ways of earning
=> Ean with your browser & extensions
=> More Legit sites at one place.

No comments

Note: Please note that we are not responsible for the links in comments. So join the sites from the comments on your own risk. Thanks

Powered by Blogger.