Android Dialog reopen after rotate device

青春壹個敷衍的年華 提交于 2019-12-06 04:59:13

Hi You have to add screen orientation support in your application manifest file.

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

And also override the following method ,

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

use create() method instead of show() method

new AlertDialog.Builder(this).create()

Try use own class that will be extends from DialogFragment

For example:

    public class QuestionDialogFragment extends DialogFragment
    {
        public final static String BF_TITLE = "QuestionDialogFragment.BF_TITLE";
        public final static String BF_QUESTION = "QuestionDialogFragment.BF_QUESTION";

        private Callback mCallback;

        public static void init(FragmentManager fragmentManager, String title, String question, Callback callback)
        {
            Bundle bundle = new Bundle();
            bundle.putString(BF_TITLE, title);
            bundle.putString(BF_QUESTION, question);

            QuestionDialogFragment dialog = new QuestionDialogFragment();
            dialog.setCallbackListener(callback);
            dialog.setArguments(bundle);
            dialog.show(fragmentManager, null);
        }

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            setCancelable(false);
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState)
        {
            Bundle bundle = getArguments();

            String title = null;
            String question = null;

            if (bundle != null)
            {
                if (bundle.containsKey(BF_TITLE))
                {
                    title = bundle.getString(BF_TITLE);
                }
                if (bundle.containsKey(BF_QUESTION))
                {
                    question = bundle.getString(BF_QUESTION);
                }
            }

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
            alertDialogBuilder.setTitle(title);
            alertDialogBuilder.setMessage(question);
            //null should be your on click listener
            alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    mCallback.success();
                    dialog.dismiss();
                }
            });
            alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {

                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    mCallback.cancel();
                    dialog.cancel();
                }
            });

            return alertDialogBuilder.create();
        }

        public void setCallbackListener(Callback callback)
        {
            this.mCallback = callback;
        }

        public static interface Callback
        {
            void success();
            void cancel();
        }
}

And use it anywhere in your code:

            QuestionDialogFragment.init(
                getFragmentManager(),
                "Some title",
                "Some question?",
                new QuestionDialogFragment.Callback()
                {
                    @Override
                    public void success()
                    {
                        // @TODO if user choice YES;
                    }

                    @Override
                    public void cancel()
                    {
                        // @TODO if user choice CANCEL;
                    }
                });

If you want create own view instead standard dialog window just instead:

  Dialog onCreateDialog(Bundle savedInstanceState) 

use

  View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

For example:

need create values/layout/your_fragment_layout.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:id="@+id/kom_purchase_dialog_root_view">

        <TextView
            android:id="@+id/kom_purchase_dialog_message_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="some text"/>

        <LinearLayout
            android:layout_gravity="bottom"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/kom_purchase_dialog_negative_button"
                android:layout_alignParentBottom="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Cancel"/>

            <Button
                android:id="@+id/kom_purchase_dialog_positive_button"
                android:layout_toRightOf="@+id/kom_purchase_dialog_negative_button"
                android:layout_alignParentBottom="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Ok"/>
        </LinearLayout>
    </FrameLayout>

and for this layout change own class as:

public class QuestionDialogFragment2 extends DialogFragment
{

    public final static String BF_TITLE = "QuestionDialogFragment.BF_TITLE";
    public final static String BF_QUESTION = "QuestionDialogFragment.BF_QUESTION";

    private Callback mCallback;

    public static void init(FragmentManager fragmentManager, String title, String question, Callback callback)
    {
        Bundle bundle = new Bundle();
        bundle.putString(BF_TITLE, title);
        bundle.putString(BF_QUESTION, question);

        QuestionDialogFragment2 dialog = new QuestionDialogFragment2();
        dialog.setCallbackListener(callback);
        dialog.setArguments(bundle);
        dialog.show(fragmentManager, null);
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setCancelable(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        Bundle bundle = getArguments();

        String title = null;
        String question = null;

        if (bundle != null)
        {
            if (bundle.containsKey(BF_TITLE))
            {
                title = bundle.getString(BF_TITLE);
            }
            if (bundle.containsKey(BF_QUESTION))
            {
                question = bundle.getString(BF_QUESTION);
            }
        }
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (view == null)
        {
            view = inflater.inflate(R.layout.your_fragment_layout, null, false);
            view.setTag(new Holder(view));
        }
        Holder holder = (Holder) view.getTag();
        holder.messageTextView.setText(question);
        holder.positiveButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mCallback.success();
            }
        });
        holder.negativeButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mCallback.cancel();
            }
        });
        return view;
    }

    public void setCallbackListener(Callback callback)
    {
        this.mCallback = callback;
    }

    public static interface Callback
    {
        void success();
        void cancel();
    }

    private final class Holder
    {
        public TextView messageTextView;
        public Button positiveButton;
        public Button negativeButton;

        private Holder(View view)
        {
            messageTextView = (TextView) view.findViewById(R.id.question_dialogfragment_message_textview);
            positiveButton = (Button) view.findViewById(R.id.question_dialogfragment_positive_button);
            negativeButton = (Button) view.findViewById(R.id.question_dialogfragment_negative_button);
        }
    }
}

and the same usage:

            QuestionDialogFragment2.init(
                getFragmentManager(),
                "Some title",
                "Some question?",
                new QuestionDialogFragment2.Callback()
                {
                    @Override
                    public void success()
                    {
                        // @TODO if user choice YES;
                    }

                    @Override
                    public void cancel()
                    {
                        // @TODO if user choice CANCEL;
                    }
                });

For both approaches will work void onSaveInstanceState(Bundle outState) and save state after rotation. I think it's better and universal approach then user the simple dialog.

cosic

It doesn't matter if Dialog or AlertDialog was used. In order to avoid closing the dialog when you rotate screen, use this code:

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!