首先,代码:
public class MyApp extends ActionBarActivity {
protected static boolean Stopped;
protected static AlertDialog alertDialog;
protected static Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
Stopped = false;
activity = this;
super.onCreate(savedInstanceState);
//...
}
@Override
protected void onStop() {
Stopped = true;
super.onStop();
if(alertDialog != null)
alertDialog.dismiss();
}
}
稍后在我的代码中:
AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.activity);
builder.setTitle("Title");
builder.setMessage("Message");
final AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (!MyApp.Stopped)
MyApp.alertDialog = null;
}
});
dialog.show();
MyApp.alertDialog = dialog;
问题是:调用onStop()
时,Stopped
为真,但在此之后,在onDismiss()
中,MyApp.Stopped
仍然是的假
有没有办法纠正这个?
在onStop()
之前调用P.S。:onDismiss()