旋转设备后 Android 对话框重新打开

标签 android dialog rotation screen-rotation

我正在编写一个非常简单的应用程序来打开我的自定义共享对话框。 XML 布局仅包含 1 个按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:gravity="center_horizontal">

    <Button android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_margin="20dip"
        android:text="Click here to open Share Dialog"
        android:onClick="onBtnShareClick"/>

</LinearLayout>

在 Activity 上,我创建了一个自定义共享对话框

public class CustomDialog extends Activity {

    private static final int SHOW_DIALOG_SHARE = 1;
    private ArrayAdapter<ShareItem> mShareAdapter;

    @Override
    protected void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        setContentView(R.layout.custom_dialog);

        final ShareItem[] items = {
            //new Item("Menu item", R.drawable.icon_assistance),
            new ShareItem("Banbe", R.drawable.ic_banbe),
            new ShareItem("Facebook", R.drawable.ic_facebook),
            new ShareItem("Twitter", R.drawable.ic_twitter),
            new ShareItem("Gmail", R.drawable.ic_gmail),
            new ShareItem("Other sharing options...", 0)
        };

        mShareAdapter = new ArrayAdapter<ShareItem>(
        this,
        android.R.layout.select_dialog_item,
        android.R.id.text1,
        items){
            public View getView(int position, View convertView, ViewGroup parent) {
                //User super class to create the View
                View v = super.getView(position, convertView, parent);
                TextView tv = (TextView)v.findViewById(android.R.id.text1);

                //Put the image on the TextView
                tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);

                //Add margin between image and text (support various screen densities)
                int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                tv.setCompoundDrawablePadding(dp5);

                return v;
            }
        };
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case SHOW_DIALOG_SHARE:
            return new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle(R.string.app_name)
            .setAdapter(mShareAdapter, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(CustomDialog.this, "Click on item " + item, Toast.LENGTH_SHORT).show();
                }
            })
            .show();
        }
        return null;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
    }

    public void onBtnShareClick(View v) {
        showDialog(SHOW_DIALOG_SHARE);
    }

    protected class ShareItem {
        public final String text;
        public final int icon;
        public ShareItem(String text, Integer icon) {
            this.text = text;
            this.icon = icon;
        }
        @Override
        public String toString() {
            return text;
        }
    }

}

当单击按钮时,我的共享对话框将打开。一切都好。

现在,我将设备旋转到纵向模式,单击按钮打开对话框。之后,按返回关闭共享对话框。 将设备旋转至横向模式。尽管我没有单击按钮,但共享对话框突然重新打开。

当我尝试使用 native 共享对话框时,我没有看到此错误。也许自定义共享对话框是原因?

谁能告诉我这里出了什么问题吗?

最佳答案

您好,您必须在应用程序 list 文件中添加屏幕方向支持。

 <activity android:name=".TestApp"
     android:label="@string/app_name"   android:configChanges="orientation">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

并且还重写以下方法,

  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
  }

关于旋转设备后 Android 对话框重新打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8816018/

相关文章:

android - 如何更改 Android 对话框背后的颜色

android - 如何在 Android 中删除对话框的标题

android - 如何使用两个日期选择器创建自定义对话框?

jquery - CSS:以div为中心旋转文本

rotation - 在 Canvas 中多次旋转单行

android - 当 Market 不工作时,Google 应用内结算返回 RESULT_BILLING_UNAVAILABLE

android - TableLayout 中的右对齐列

android - 我可以在自定义选项卡 View 中使用默认选项卡样式吗?

java - 在数据库中添加数据的最佳技术

swift - UIView每次旋转都会错位