Display back button on action bar
well this is simple one to show back button
actionBar.setDisplayHomeAsUpEnabled(true);
and then you can custom the back event at onOptionsItemSelected
case android.R.id.home:
this.finish();
return true;
I think onSupportNavigateUp()
is the best and Easiest way to do so, check the below steps. Step 1 is necessary, step two have alternative.
Step 1 showing back button: Add this line in onCreate()
method to show back button.
assert getSupportActionBar() != null; //null check
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //show back button
Step 2 implementation of back click: Override this method
@Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
thats it you are done
OR Step 2 Alternative: You can add meta to the activity in manifest file as
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
Edit: If you are not using AppCompat
Activity then do not use support
word, you can use
getActionBar().setDisplayHomeAsUpEnabled(true); // In `OnCreate();`
// And override this method
@Override
public boolean onNavigateUp() {
finish();
return true;
}
Thanks to @atariguy for comment.