欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Display back button on action bar and back event

程序员文章站 2022-06-03 13:01:20
...
原文地址:
https://*.com/a/37185334

I think onSupportNavigateUp() is best and Easiest way to do so
check the code below
if you want it programmatically Add this line in onCreate() method
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 
Note: make sure Actionbar is not null.

And Override this method
@Override
public boolean onSupportNavigateUp(){  
    finish();  
    return true;  
}

thats it
OR Non-programmatically you can add meta to the activity in manifest file as
<meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="MainActivity" />


Other solution:
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onBackPressed();
    }
});