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();
}
}
}