Kanth
Kanth

Reputation: 6751

Alert dialog pops up even after dismissing it

I have come across a typical problem and it seems strange to me. Details are something like this - On the activity in my app, there are edittexts and submit button. After filling the data in the edittexts, user can click submit button. After clicking submit button, based on the values that are entered by the user, either of the two alert dialogs are shown. One is success and the other one is failed.

The thing is when the user enters invalid data and clicks submit button, the failed alert dialog gets opened. I have a button(OK) on the failed alert dialog, after clicking it I wrote dialog.dismiss(); to make it disappear, so that user can recheck the data and can modify. But the problem is while rechecking & modifying the data if he changes the orientation, then again the failed alert dialog is popping up even without clicking submit button. Please suggest.

Extra Details(though probably not necessary for this problem): While changing orientation the activity is recreated. So, I am saving the current data in the onSavedInstanceState() and retrieving it in onCreate() method to set back the values in the edittexts. Everything works fine, but once clicking on submit button, the respective alert dialog appears. Then after changing orientation the dialog is again popping up. I am sure that I wrote showDialog(1); in the onClick() method but then again why control is going back into onClick and showing that alert dialog even without clicking.

protected Dialog onCreateDialog(int id) {
   switch(id){                 
      case 0:          
        return new AlertDialog.Builder(this)
                      .setMessage("Success!") 
                      .setIcon(R.drawable.success)
                      .setPositiveButton("OK",
                      new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();                   
                         }
                      }).show();
       case 1:
           return new AlertDialog.Builder(this)
                      .setMessage("Failed")
                      .setIcon(R.drawable.failure)
                      .setPositiveButton("OK",
                       new DialogInterface.OnClickListener() {

                           @Override
                           public void onClick(DialogInterface dialog, int which) {
                              dialog.dismiss();     
                              return;
                           }
                        }).show();   
           }
           return null;
       }

Here is the method that makes alert dialog show.

public void onClick(View v) {
   switch (v.getId()) {    
      //Here there are other cases too.
      case R.id.submit:
        getEditTexts();
        validator();
      break;
   }
}

public void validator() {              
    if(generator.receiveVal(0,0,sudo)) {
       showDialog(0);
     }
    else if(!generator.receiveVal(0,0,sudo)) {
       showDialog(1);
    }
}

Upvotes: 2

Views: 3348

Answers (2)

RedMill Ltd
RedMill Ltd

Reputation: 21

This is just an idea but it seems the issue is that onOrientation is trying to re-draw the activity.

Try something like the following:

You can add this to the activity declaration in the manifest:

android:configChanges="orientation"

so it looks like

<activity android:label="@string/app_name" 
    android:configChanges="orientation" 
    android:name=".your.package">

The matter is that the system destroys the activity when a change in the configuration occurs. See ConfigurationChanges.

So putting that in the configuration file avoids the system to destroy your activity. Instead it invokes the onConfigurationChanged(Configuration) method.

Hope this helps.

Upvotes: 2

Enthusiast
Enthusiast

Reputation: 249

Just try replacing .create() in the place of .show(). In your case like this:

case 1:
               return new AlertDialog.Builder(this)

               .setMessage("Failed")
               .setIcon(R.drawable.failure)
               .setPositiveButton("OK",
                       new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            dialog.dismiss();

                          return;
                        }
                    }).create();   //Here replaced .show with .create()

Upvotes: 3

Related Questions