Reputation: 4609
I have not seen a specific answer to this question so please do flag this as a duplicate if you can find the duplicate.
I am wondering how does one make the back button of the Action Bar button act as the system back button. So when an action bar is displayed and there is the application icon in the top left with the back arrow, how does one make that act as the system (hardware sometimes) back button? Here is an example of the button I am talking about.
Upvotes: 0
Views: 1046
Reputation: 4609
So in order to do this you will need to on your activitys UI setup call these.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
and then if you do not want text, like that example you need to call
getSupportActionBar().setDisplayShowTitleEnabled(false);
Then in your activity you need to create a method that looks like this.
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int theId = item.getItemId();
if (theId == android.R.id.home) {
callCleanupActivityMethod();
finish();
}
return true;
}
That is a callback listener that is immediately put on the activity when an action bar has been put on the activity. That function then grabs the id of the item that was clicked, checks to see if it was the home button, then you need to clean any objects or processes that need to be stopped then call finish() which will make the activity finish. Finally return true.
When looking at the comments you can probably see that a member suggested just calling onBackPressed, that may work for some applications but not all hence the call to finish. I do recommend trying it first though.
Cheers
Upvotes: 1