jaimito
jaimito

Reputation: 145

Alert dialog pops up every time I rotate

I'm doing an app and I'm using Alert Dialog but after I pressed a button and I rotate, the alert dialog pops up again. I was trying to do this but didn't work.

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Alert Dialog")
    .setMessage("Startup Button Visibility:")
    .setPositiveButton("Hidden", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) { 
            if(buttonVisible == true)
            {
                myLayout2.setVisibility(View.INVISIBLE);
            }
            else
                myLayout2.setVisibility(View.VISIBLE);


          SharedPreferences myPrefs = getPreferences(MODE_PRIVATE);
          boolean storedPreference = myPrefs.getBoolean("Skip", true);
          SharedPreferences.Editor editor = myPrefs.edit();
          if(storedPreference != false)
          {
              storedPreference= true;
          }
          editor.putBoolean("Skip", storedPreference);
          editor.commit();
        }
     })

     .show();

Upvotes: 0

Views: 74

Answers (2)

aslam khan
aslam khan

Reputation: 85

Add this

SharedPreferences myPrefs = getPreferences(MODE_PRIVATE); 

boolean storedPreference = myPrefs.getBoolean("Skip", true); 

if(!storedPreference){
   dialog.show();}

Upvotes: 0

lindenrovio
lindenrovio

Reputation: 357

The activity is destroyed and recreated every time you rotate. See Android lifecycle: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

So if you put your code in the creation code, then it will be called to show a dialog every time you rotate.

Upvotes: 1

Related Questions