Reputation: 504
I used Toolbar component in AppCompat library instead of Default Actionbar.
Compile time: I get compile-error that cannot find symbol android.R.id.home
public void setupActionBar() {
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.action_help) {
showHelp();
return true;
}else if(id == android.R.id.home){
Log.d(TAG, "Back Button clicked!");
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Upvotes: 5
Views: 1789
Reputation: 36
android.R.id.home was introduced in API level 11. here is more detail: https://stackoverflow.com/a/18719090/2178694
Upvotes: 2
Reputation: 1747
add this to your activity.
@Override
public void onBackPressed() {
// your code.
this.finish();
}
Upvotes: 1