I'm working on an android app and recently, I discovered that the default back button onBackPressed()
produces a back behaviour, while the getSupportActionBar().setDisplayHomeAsUpEnabled(true)
produces an up behaviour. And the two have significant differences.
I was wondering if I can simulate the up behaviour when I press the hardware back button, so that it navigates up instead of back . Thanks.
You can do this.
@Override
public void onBackPressed() {
NavUtils.navigateUpFromSameTask(this);
}
you can do by this
override the onBackPressed() method into your Activity like this way
public void onBackPressed(){
// do something here and don't write super.onBackPressed()
}
override the onKeyDown() method
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
Hope it helps you
You can do this
Overide onOptionsItems Selected
Create a switch case of android.R.id.home Call method onBackPressed();
here is the code
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
default:
return super.onOptionsItemSelected(item);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.