0
    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 = getPreferences(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();
                        pinDialogState.put("click1", false);
                    }
                });

        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);
        }
    }

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

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.d("Test3", "onResume()");
        alertCreate.show();
    }

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

}

我正在为如何在从 onPause() 中解除警报对话框后重新显示它的逻辑而苦苦挣扎。我正在考虑共享首选项,但我怎么知道在活动被破坏之前正在显示对话框,现在用户回到应用程序,我必须显示对话框。我还听说,在活动被销毁后,简单地调用 alertDialog.show() 将不起作用。我能做些什么?谢谢你。

4

2 回答 2

2

Maybe Crouton works, i dont know, but try:

http://www.grokkingandroid.com/useful-android-libraries-crouton/

Crouton is Nofitication Library, simple, easy, fast!

于 2012-11-20T15:56:25.410 回答
1

您将使用共享首选项是对的。

onPouse()方法中将您的警报消息保存为标志并打开它onResume()

public class Test3 extends Activity {
....
 SharedPreferences mPrefs = context.getSharedPreferences("MyPrefs", 0);

 ....
 @Override
protected void onPause() {
    super.onPause();
    Log.d(tag, "onPause()");


         SharedPreferences.Editor editor = mPrefs.edit();
         editor.putBoolean("isShowed", alertCreate );
         editor.commit();

    if (alertCreate != null) {
        alertCreate.dismiss();
        editor.putBoolean(click1, true);
    }
}

 ....

 @Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.d("Test3", "onResume()");

     boolean isShow = mPrefs.getBoolean("isShowed", false);

     if(isShow == true){
        alertCreate.show();
        .....
     }

}


}
于 2012-11-20T16:07:32.860 回答