问题
What is happening::
- dialog is popped
- I click ok - > it leads me to settings screen
- Now i press back button
- When i come back dialog button is still there
- again i click ok
- again it leads me to settings screen
- this repeats, dialog is never closed
What i am trying to do:
- dialog is popped
- I click ok - > it leads me to settings screen
- Now i press back button
- When i come back dialog button should have closed
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createInstances();
//set up notitle
requestWindowFeature(Window.FEATURE_NO_TITLE);
//set up full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_splash_screen);
}
@Override
protected void onStart() {
super.onStart();
open(getResources().getString(R.string.location_not_enabled));
}
public void open(String custMsg){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage(custMsg);
alert.setCancelable(false);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
locationValidationDone=true;
dialog.dismiss();
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
alert.show();
}
回答1:
dialog is used inonstart()
please use in oncreate()
,
when your back to your actvity it's called the onstart()
and onresume()
,
please read the activity lifecycle
lifecycle
@Override
protected void onStart() {
super.onStart();
if(!start_dialog)
{
start_dialog = true;
open(getResources().getString(R.string.location_not_enabled));
}
}
回答2:
In the ok button code instead of
dialog.dismiss();
use it
alert.dismiss();
来源:https://stackoverflow.com/questions/27577887/dialog-is-not-closed-dialog-dismiss-when-navigating-backand-forth-to-settings