mehmet
mehmet

Reputation: 1588

AlertDialog recreating after Rotating

Well I am trying to use Alertdialog from Docs. but there is a problem. if I rotate phone alert dialog is recreated and there are two alertdialog appears. How can prevent re and re create the alertDialog.

this is my code:

   public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

and this is the showing method:

public void confirmFireMissiles() {
    DialogFragment newFragment = new FireMissilesDialogFragment();
    newFragment.show(getSupportFragmentManager(), "missiles");
}

Upvotes: 0

Views: 500

Answers (2)

Neetu Shrivastava
Neetu Shrivastava

Reputation: 229

Add this

android:configChanges="orientation|screenSize" property inside your Activity tag in manifest file.

Upvotes: -1

schabluk
schabluk

Reputation: 708

Check if savedInstanceState is set:

   public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        if (savedInstanceState == null) {
            // create new dialog
        }
   }

Upvotes: 2

Related Questions