活动通过onDestroy()后如何恢复警报对话框?

时间:2012-11-21 14:55:35

标签: android android-alertdialog

package com.example.test3;

import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Test3 extends Activity {

    Button testShowPinDialogButton;
    public AlertDialog alertCreate;
    AlertDialog.Builder alert;
    private HashMap<String, Boolean> pinDialogState;
    EditText input;
    Context context;
    private String tag = "Test3";
    private String click1 = "click1";
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test3);
        context = this;
        sharedPreferences = context.getSharedPreferences(click1, MODE_PRIVATE);
        editor = sharedPreferences.edit();
        testShowPinDialogButton = (Button) findViewById(R.id.testShowPinDialogBbutton);
        testShowPinDialogButton.setOnClickListener(showPinDialog);
        pinDialogState = new HashMap<String, Boolean>();
        Log.d(tag, "onCreate()");
    }

    private OnClickListener showPinDialog = new OnClickListener() {

        @Override
        public void onClick(View v) {
            launchDialog();
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_test3, menu);
        return true;
    }

    protected void launchDialog() {
        alert = new AlertDialog.Builder(context);
        alert.setTitle("Title");
        alert.setMessage("Message");

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

            }
        });

        alert.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                });

        Log.d(tag, "launchDialog()");
        alertCreate = alert.create();
        alertCreate.show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(tag, "onPause()");
        if (alertCreate != null) {
//          alertCreate.dismiss();
            editor.putBoolean(click1, true);
            editor.commit();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.d("Test3", "onResume()");
        boolean isShown = sharedPreferences.getBoolean(click1, false);
        if (isShown) {
//          alertCreate.show();
//          launchDialog();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Test3", "onDestroy()");
    }

}

如果我回家,警告对话框就会停留在那里。因此,如果我单击“显示”对话框按钮然后回到主页并返回应用程序,则对话框仍然存在。但是,如果我在开发者选项下打开“不要保留活动”选项,则不会出现警告对话框。是否可以将它保留在那里?

1 个答案:

答案 0 :(得分:0)

执行此操作的最简单方法(现在已弃用)是使用showDialog(int)并覆盖onCreateDialog(int)方法并提供自定义实现。这将显示您的对话框(duh),并在重新创建活动时重新显示它(例如方向切换)。

Original (non fragment) dialog tutorial

一旦你开始工作,你可能想尝试使用DialogFragment,这是继续这样做的方法。

Documentation on modern Fragment Dialogs