旋转屏幕时,AlertDialog消失

时间:2014-05-25 14:24:37

标签: android android-alertdialog

在我的活动中,我有一个AlertDialog,在按下按钮时显示。我注意到,当屏幕旋转时,它会使应用程序崩溃。为了解决这个问题,我在onCreateDialog()方法中添加了这行代码:setRetainInstance(true);

之后,活动不再崩溃,但AlertDialog消失了。

我在另一个活动上做了同样的事情,默认情况下显示一个Dialog,当某个条件为true并且它在屏幕旋转时成功显示的Dialog,因此我担心我的问题在于单击按钮时会调用Dialog。

我还没有尝试过任何明显的解决方法:

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

因为文档没有建议。

有没有办法在不需要重新按下按钮的情况下保持Dialog?

这是我的活动:

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

}

2 个答案:

答案 0 :(得分:1)

如您所知,Android系统会在屏幕方向发生变化时破坏活动。处理此问题的正确方法是通过您的活动管理对话框。

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
}

您可以使用

显示对话框
 showDialog(yourDialogID);

答案 1 :(得分:0)

我找到了解决这个问题的非常难看的方法。基本上当我按下按钮时,我将静态变量设置为真。

然后,在onCreate中,我检查该变量,如果它是真的,我重新创建对话框。

我绝对相信必须有更正确的方法来做到这一点,但我真的无法找到它。

相关问题