0

In my activity I have an AlertDialog that shows up when a button is pressed. I noticed that, when the screen is rotated, it crashed the app. To solve this problem I added this line of code in the onCreateDialog() method: setRetainInstance(true);.

After that the activity doesn't crash anymore but the AlertDialog disappears.

I did the same thing on another activity that shows by default a Dialog when a certain condition is true and the Dialog it's successfully showed at the rotating of the screen, therefore I'm afraid that my problem lies on the fact that the Dialog is called when the button is clicked.

I didn't yet tried any of the manifest workaround like this:

<activity               
         android:name="YourActivityName"   
         android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
         android:windowSoftInputMode="adjustPan">
     </activity>

since it's not advised by the documentation.

Is there any way to keep the Dialog up without the need to re-press the button?

Here's my activity:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;

public class PiattoActivity extends Activity {

    MenuQuantityDialogFragment dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.piatto_layout);

        TextView tv_nome = (TextView) findViewById(R.id.piatto_name);
        TextView tv_ingredients = (TextView) findViewById(R.id.piatto_ingredients);
        TextView tv_description = (TextView) findViewById(R.id.piatto_description);
        TextView tv_price = (TextView) findViewById(R.id.piatto_price);

        Button b_add = (Button) findViewById(R.id.piatto_add);
        Button b_quantity = (Button) findViewById(R.id.piatto_quantity);

        String i = "Ingrediente;";
        String d = "Nel mezzo del cammin di nostra vita mi ritrovai per una selva oscura ché la diritta via era smarrita.";

        tv_nome.setText("Nome piatto");
        tv_ingredients.setText(i + i + i + i + i + i);
        tv_description.setText(d);
        tv_price.setText("10€");

        b_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog = new MenuQuantityDialogFragment();
                FragmentManager fm = getFragmentManager();
                dialog.show(fm, "frag_name");
            }
        });


    }

    public class MenuQuantityDialogFragment extends DialogFragment {
        //crea il dialogfragment
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            setRetainInstance(true);
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            NumberPicker np = new NumberPicker(PiattoActivity.this);

            np.setMinValue(1);
            np.setMaxValue(10);
            np.setWrapSelectorWheel(false);
            np.setValue(2);

            builder.setView(np);

            builder.setMessage(R.string.piatto_dialog_message)
                   .setTitle(R.string.piatto_dialog_title)
                   .setPositiveButton(R.string.piatto_dialog_ok, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dismiss();
                       }
                   })
                   .setNegativeButton(R.string.piatto_dialog_annulla, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dismiss();
                       }
                   });
            return builder.create();
        }
    }

}
Higure
  • 235
  • 1
  • 5
  • 19

2 Answers2

1

As you know that Android system will destroy activity when screen orientation changed. The correct way to handle this is to manage dialogs through your activity.

protected Dialog onCreateDialog(int id) {
    // create and return your dialog instance here
    AlertDialog dialog = new AlertDialog.Builder(context)
        .setTitle(title)
        .setIcon(R.drawable.indicator_input_error)
        .setMessage(message)
        .create();
    dialog.setButton(
            DialogInterface.BUTTON_POSITIVE,    
            context.getString(R.string.OK),
            (DialogInterface.OnClickListener) null);
    return dialog;
}

protected void onPrepareDialog(int id, Dialog dialog) {
    // You dialog initialization code here
}

You can show dialog using

 showDialog(yourDialogID);
Matt
  • 74,352
  • 26
  • 153
  • 180
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
0

I found a very ugly way to solve this. Basically when I press the button I set a static variable true.

Then, in the onCreate, i check that variable and, if it's true, i re-create the dialog.

I'm absolutely sure there must be a more correct way to do this, but I seriously can't find it.

Higure
  • 235
  • 1
  • 5
  • 19