【问题标题】:alertdialog is disappears while reload the activity in android在android中重新加载活动时警报对话框消失
【发布时间】:2015-09-17 20:51:54
【问题描述】:

我必须开发一个 android 应用程序。

我已经创建了一个警报对话框。如果我必须旋转方向意味着警报对话框消失了。

但我希望在方向改变时显示警告对话框。

@Override
   public void onConfigurationChanged ( Configuration newConfig )
  {
      super.onConfigurationChanged(newConfig);
    try
    {
        MainActivity.editCalled = true;
        Intent in = new Intent(AndroidListFragmentActivity.this, AndroidListFragmentActivity.class);
        startActivity(in);
        finish();
    }
     catch (Exception e)
    {
        e.printStackTrace();
    }
}

这里我使用了两个片段...

在具有一个警报对话框的第二个片段中:

    ImageView share = (ImageView) findViewById(R.id.imageView5);
    share.setOnClickListener(new OnClickListener()
      {
        public void onClick ( View v )
        {
            final CharSequence[] items =
            {
                    "Facebook", "Twitter", "Email"
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(SubCate.this);
            builder.setTitle("Share Via:");
            builder.setItems(items, new DialogInterface.OnClickListener()
            {
                public void onClick ( DialogInterface dialog , int item )
                {
                    if (items[item] == "Facebook")
                    {

                        onFacebookClick();
                    }
                    if(items[item] == "Twitter"){

                        onClickTwitt();
                       } 
                    if (items[item] == "Email")
                    {
                        Intent email = new Intent(Intent.ACTION_SEND);
                        email.setType("message/rfc822");

                        email.putExtra(Intent.EXTRA_EMAIL, new String[]
                        {
                                ""
                        });
                        email.putExtra(Intent.EXTRA_SUBJECT, _Substring);
                        email.putExtra(Intent.EXTRA_TEXT, ContentEmail);
                        startActivity(Intent.createChooser(email, "Choose an Email client :"));

                    }
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
      });
}
Here only i  have facing above problem..please give me solution for these ???

【问题讨论】:

  • 尝试在您的清单中添加android:configChanges="orientation" 以进行相应的活动并检查。
  • 不鼓励在 Android 中设置 android:configChanges="orientation"

标签: android android-alertdialog android-orientation


【解决方案1】:

在 Android 中设置 android:configChanges="orientation"not encouraged。您可以先在片段中声明Alertdialog,然后使用onSavedInstanceState

AlertDialog alert;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.yourid, container, false);

        if(savedInstanceState != null && savedInstanceState.getBoolean("alertShown",true)) {
            showDialog(view.getContext());
        }
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    if(alert != null && alert.isShowing()) {
        // close dialog to prevent leaked window
        alert.dismiss();
        outState.putBoolean("alertShown", true);
    }
}

// put all creating dialog stuff in a single method
protected void showDialog(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    ...
    alert = builder.create();
    alert.show();
}

【讨论】:

  • 您好,我已将您的代码集成到我的项目中。现在更改方向时,我的警报对话框也会消失。这是我更新的代码:pastie.org/private/rcyh3pyzm9e575vwvn9dq
  • 不要将这些代码放入onCreate()。像我一样把它们放在onSavedInstanceState()
【解决方案2】:

正如这里很多人的建议,

android:configChanges="keyboardHidden|orientation"

不是解决方案。充其量只是一个黑客。处理此问题的正确方法是通过您的活动管理对话。您需要在活动代码中覆盖一些方法,如下所示:

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

完成此操作后,您会看到如果发生配置更改,您的对话框也会重新创建。最好的部分是您的活动将为您管理对话。如果您执行繁重的初始化,它将尽可能重用,从而减少对话框加载时间。

【讨论】:

  • 是的,我已经在清单文件中添加了。但是我的警报对话框在更改方向时消失了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多