0

我正在尝试在退出我的应用程序的活动时保存数据。

@Override
public void onBackPressed() {

super.onBackPressed();
if(loadInboxPreferencesCurrent()==false){
    showAlertUserDialogInbox();
}

}

public void showAlertUserDialogInbox() {

    AlertDialog.Builder builderMiniAlert = new AlertDialog.Builder(this);
    builderMiniAlert.setMessage("Do you want to exit without saving your App. Inbox Settings..\n\nDo you want to Save now. ?")
               .setCancelable(false); // disallow user to hit the 'back' button

    builderMiniAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            //Save settings here
            onBackPresedNSave();

        }
    });
    builderMiniAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
        Toast.makeText(getApplicationContext(), "To set your App. Inbox Settings later, press menu key and select Save Inbox Settings.", Toast.LENGTH_LONG).show();
            arg0.dismiss();
        }
    });
    builderMiniAlert.setTitle("Ultimate Sms Inbox Alert...");

    builderMiniAlert.show();

}

现在真正的问题是在按下返回按钮后,对话框会显示一秒钟并消失,而无需用户单击是/否按钮。

4

1 回答 1

0

删除super.onBackPressed

@Override
public void onBackPressed() {

    super.onBackPressed(); //remove this because this will close the activity
    if(loadInboxPreferencesCurrent()==false){
         showAlertUserDialogInbox();
    }

}

并确保您在其他地方处理完成活动,例如在对话框中单击正/负按钮后。

我不确定您尝试做的是否是保存数据的最佳方法。

您可能应该查看活动的 onSaveInstanceState() 但如果您的目标是仅在用户确认后保存,那么您的第一种方法就可以了。

于 2013-07-01T08:22:26.890 回答