Art*_*kii 23 android button exit back android-activity
我想警告用户,如果背压将完成堆栈上的最后一个活动,从而退出应用程序.我想在接下来的几秒钟内弹出一个小吐司并检测到第二次背压,然后才打电话finish().
我已经编写了背压检测编码onBackPressed(),但是我找不到一种明显的方法来查看背堆上剩下多少活动.
谢谢.
Dar*_*opp 22
reddit是有趣的应用程序通过覆盖onKeyDown方法来做到这一点:
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK && isTaskRoot()) {
//Ask the user if they want to quit
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.quit)
.setMessage(R.string.really_quit)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Stop the activity
finish();
}
})
.setNegativeButton(R.string.no, null)
.show();
return true;
}
else {
return super.onKeyDown(keyCode, event);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12445 次 |
| 最近记录: |