我一直试图表现出"你想退出吗?" 用户尝试退出活动时的对话框类型.
但是我找不到合适的API挂钩. Activity.onUserLeaveHint()最初看起来很有希望,但我找不到阻止活动完成的方法.
我有一个活动,其中包含几个用户可编辑的项目(EditText字段,RatingBar等).如果按下后退/主页按钮并且已经进行了尚未保存的更改,我想提示用户.通过阅读android文档后,似乎这段代码应该放在onPause方法中.我已经尝试在onPause中放置一个AlertDialog,但是对话框会显示出来,然后立即关闭,因为没有任何东西阻止暂停完成.
这是我到目前为止所提出的:
@Override
protected void onPause() {
super.onPause();
AlertDialog ad = new AlertDialog.Builder(this).setMessage(
R.string.rating_exit_message).setTitle(
R.string.rating_exit_title).setCancelable(false)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// User selects OK, save changes to db
}
}).setNeutralButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// User selects Cancel, discard all changes
}
}).show();
}
Run Code Online (Sandbox Code Playgroud)
我是在正确的轨道还是有另一种方式来完成我在这里要做的事情?任何帮助都会很棒!