[英]Android How to prevent back button from quitting application (after home button pressed)
I have an issue whereby, the back button works fine. 我有个问题,后退按钮可以正常工作。 Unless you push the home button, then re-enter the application, then push the back button again.
除非您按下主页按钮,否则请重新输入应用程序,然后再次按下后退按钮。 It then quits the App, because their is no task trail (of activities)
然后退出应用程序,因为它们不是(活动的)任务跟踪
Here is my colleagues code, of which I am trying to fix. 这是我的同事代码,我正在尝试对其进行修复。 Android.R.id.home is the problematic soft back button, although same thing is happening with OS back button.
Android.R.id.home是有问题的软后退按钮,尽管OS后退按钮也发生了同样的事情。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case android.R.id.home:
activity.finish();
return true;
case R.id.menu_paymentLocs:
intent = new Intent(activity, PaymentLocationsPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
activity.startActivity(intent);
return true;
case R.id.menu_feedback:
intent = new Intent(activity, FeedbackPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
activity.startActivity(intent);
return true;
case R.id.menu_about:
intent = new Intent(activity, AboutPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
activity.startActivity(intent);
return true;
case R.id.menu_changeconsumer:
new SelectConsumerDialogFragment().show(getFragmentManager(), "select_consumer");
return true;
case R.id.menu_logout:
intent = new Intent(activity, SplashPage.class);
myMeter.logout();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
activity.startActivity(intent);
return true;
}
return true;
}
To prevent the back button from doing anything predefined you need to override the onbackpressed()
method 为了防止后退按钮执行任何预定义的操作,您需要重写
onbackpressed()
方法
try this if you are using api level 2.0 or higher 如果您使用的是api级2.0或更高版本,请尝试此操作
@Override
public void onBackPressed() {
// Do Here what ever you want do on back press;
}
删除所有intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.