【问题标题】:AlertDialog disappears when rotating the screen [duplicate]旋转屏幕时AlertDialog消失[重复]
【发布时间】:2014-07-14 09:50:15
【问题描述】:

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

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

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

我还没有尝试过任何类似的清单解决方法:

<activity               
         android:name="YourActivityName"   
         android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
         android:windowSoftInputMode="adjustPan">
     </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();
        }
    }

}

【问题讨论】:

  • 为什么不在屏幕旋转之前在bundle上传递一个布尔变量,然后在从bundle中的数据旋转后再次显示对话框
  • @Der Golem,不完全是,我正在使用 DialogFragment。

标签: android android-alertdialog


【解决方案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);

【讨论】:

  • 通过这种方式我不使用 DialogFragment,但应该避免,阅读 android 文档。
  • 您在哪里找到了示例中的DialogFragment
  • 正是我的意思,android 文档建议使用DialogFragment 而不是直接实例化Dialog
  • 现在已弃用,取而代之的是 DialogFragment
【解决方案2】:

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

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

我绝对确定一定有更正确的方法可以做到这一点,但我真的找不到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多