Dialog is not closed dialog.dismiss() when navigating backand forth to settings screen

僤鯓⒐⒋嵵緔 提交于 2019-12-25 01:39:44

问题


What is happening::

  1. dialog is popped
  2. I click ok - > it leads me to settings screen
  3. Now i press back button
  4. When i come back dialog button is still there
  5. again i click ok
  6. again it leads me to settings screen
  7. this repeats, dialog is never closed

What i am trying to do:

  1. dialog is popped
  2. I click ok - > it leads me to settings screen
  3. Now i press back button
  4. 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 lifecyclelifecycle

@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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!