1cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/*
2cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Copyright (C) 2011 The Android Open Source Project
3cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
4cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you may not use this file except in compliance with the License.
6cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You may obtain a copy of the License at
7cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
8cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
10cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
11cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * See the License for the specific language governing permissions and
14cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * limitations under the License.
15cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
16cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
17cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpackage android.support.v4.app;
18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
198e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikasimport static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
208e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas
21ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powellimport android.app.Activity;
22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.app.Dialog;
23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.Context;
24cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.DialogInterface;
25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Bundle;
26a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.IntDef;
27a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.NonNull;
2891d6ad872f1dab57871e92dd097e35c1e59aeb26Scott Kennedyimport android.support.annotation.Nullable;
29c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo;
30a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.StyleRes;
31cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.LayoutInflater;
32cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View;
33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.ViewGroup;
34cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.Window;
35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.WindowManager;
36cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
37a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport java.lang.annotation.Retention;
38a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport java.lang.annotation.RetentionPolicy;
39a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye
40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/**
41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.app.DialogFragment}.
42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0.  When running
43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try
44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to switch to the framework's implementation.  See the framework SDK
45cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview.
46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic class DialogFragment extends Fragment
48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
50a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    /** @hide */
518e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas    @RestrictTo(LIBRARY_GROUP)
52a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    @IntDef({STYLE_NORMAL, STYLE_NO_TITLE, STYLE_NO_FRAME, STYLE_NO_INPUT})
53a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    @Retention(RetentionPolicy.SOURCE)
54a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    private @interface DialogStyle {}
55a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye
56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Style for {@link #setStyle(int, int)}: a basic,
58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * normal dialog.
59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static final int STYLE_NORMAL = 0;
61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Style for {@link #setStyle(int, int)}: don't include
64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * a title area.
65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
66cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static final int STYLE_NO_TITLE = 1;
67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
68cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
69cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Style for {@link #setStyle(int, int)}: don't draw
70cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * any frame at all; the view hierarchy returned by {@link #onCreateView}
71cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is entirely responsible for drawing the dialog.
72cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static final int STYLE_NO_FRAME = 2;
74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Style for {@link #setStyle(int, int)}: like
77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #STYLE_NO_FRAME}, but also disables all input to the dialog.
78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * The user can not touch it, and its window will not receive input focus.
79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static final int STYLE_NO_INPUT = 3;
81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private static final String SAVED_DIALOG_STATE_TAG = "android:savedDialogState";
83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private static final String SAVED_STYLE = "android:style";
84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private static final String SAVED_THEME = "android:theme";
85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private static final String SAVED_CANCELABLE = "android:cancelable";
86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private static final String SAVED_SHOWS_DIALOG = "android:showsDialog";
87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private static final String SAVED_BACK_STACK_ID = "android:backStackId";
88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    int mStyle = STYLE_NORMAL;
90cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    int mTheme = 0;
91cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mCancelable = true;
92cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mShowsDialog = true;
93cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    int mBackStackId = -1;
94cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
95cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    Dialog mDialog;
96ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    boolean mViewDestroyed;
97ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    boolean mDismissed;
98ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    boolean mShownByMe;
99cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public DialogFragment() {
101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Call to customize the basic appearance and behavior of the
105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * fragment's dialog.  This can be used for some common dialog behaviors,
106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * taking care of selecting flags, theme, and other options for you.  The
107cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * same effect can be achieve by manually setting Dialog and Window
108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * attributes yourself.  Calling this after the fragment's Dialog is
109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * created will have no effect.
110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param style Selects a standard style: may be {@link #STYLE_NORMAL},
112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #STYLE_NO_TITLE}, {@link #STYLE_NO_FRAME}, or
113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #STYLE_NO_INPUT}.
114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param theme Optional custom theme.  If 0, an appropriate theme (based
115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * on the style) will be selected for you.
116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
117a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    public void setStyle(@DialogStyle int style, @StyleRes int theme) {
118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStyle = style;
119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mStyle == STYLE_NO_FRAME || mStyle == STYLE_NO_INPUT) {
120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mTheme = android.R.style.Theme_Panel;
121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (theme != 0) {
123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mTheme = theme;
124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Display the dialog, adding the fragment to the given FragmentManager.  This
129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is a convenience for explicitly creating a transaction, adding the
130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * fragment to it with the given tag, and committing it.  This does
131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <em>not</em> add the transaction to the back stack.  When the fragment
132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is dismissed, a new transaction will be executed to remove it from
133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the activity.
134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param manager The FragmentManager this fragment will be added to.
135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param tag The tag for this fragment, as per
136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link FragmentTransaction#add(Fragment, String) FragmentTransaction.add}.
137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void show(FragmentManager manager, String tag) {
139ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        mDismissed = false;
140ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        mShownByMe = true;
141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        FragmentTransaction ft = manager.beginTransaction();
142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ft.add(this, tag);
143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ft.commit();
144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Display the dialog, adding the fragment using an existing transaction
148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * and then committing the transaction.
149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param transaction An existing transaction in which to add the fragment.
150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param tag The tag for this fragment, as per
151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link FragmentTransaction#add(Fragment, String) FragmentTransaction.add}.
152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return Returns the identifier of the committed transaction, as per
153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link FragmentTransaction#commit() FragmentTransaction.commit()}.
154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int show(FragmentTransaction transaction, String tag) {
156ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        mDismissed = false;
157ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        mShownByMe = true;
158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        transaction.add(this, tag);
159ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        mViewDestroyed = false;
160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mBackStackId = transaction.commit();
161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mBackStackId;
162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Dismiss the fragment and its dialog.  If the fragment was added to the
166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * back stack, all back stack state up to and including this entry will
167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * be popped.  Otherwise, a new transaction will be committed to remove
168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the fragment.
169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dismiss() {
171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        dismissInternal(false);
172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
174ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    /**
175ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell     * Version of {@link #dismiss()} that uses
176ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell     * {@link FragmentTransaction#commitAllowingStateLoss()
177ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell     * FragmentTransaction.commitAllowingStateLoss()}. See linked
178ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell     * documentation for further details.
179ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell     */
180ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    public void dismissAllowingStateLoss() {
181ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        dismissInternal(true);
182ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    }
183ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell
184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void dismissInternal(boolean allowStateLoss) {
185ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        if (mDismissed) {
186ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            return;
187ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        }
188ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        mDismissed = true;
189ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        mShownByMe = false;
190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDialog != null) {
191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDialog.dismiss();
192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDialog = null;
193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
194ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        mViewDestroyed = true;
195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBackStackId >= 0) {
196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            getFragmentManager().popBackStack(mBackStackId,
197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    FragmentManager.POP_BACK_STACK_INCLUSIVE);
198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mBackStackId = -1;
199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            FragmentTransaction ft = getFragmentManager().beginTransaction();
201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            ft.remove(this);
202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (allowStateLoss) {
203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                ft.commitAllowingStateLoss();
204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            } else {
205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                ft.commit();
206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2098e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas
210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Dialog getDialog() {
211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mDialog;
212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
214a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    @StyleRes
215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int getTheme() {
216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mTheme;
217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Control whether the shown Dialog is cancelable.  Use this instead of
221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * directly calling {@link Dialog#setCancelable(boolean)
222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Dialog.setCancelable(boolean)}, because DialogFragment needs to change
223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * its behavior based on this.
224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cancelable If true, the dialog is cancelable.  The default
226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is true.
227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setCancelable(boolean cancelable) {
229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mCancelable = cancelable;
230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDialog != null) mDialog.setCancelable(cancelable);
231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Return the current value of {@link #setCancelable(boolean)}.
235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean isCancelable() {
237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mCancelable;
238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Controls whether this fragment should be shown in a dialog.  If not
242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * set, no Dialog will be created in {@link #onActivityCreated(Bundle)},
243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * and the fragment's view hierarchy will thus not be added to it.  This
244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * allows you to instead use it as a normal fragment (embedded inside of
245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * its activity).
246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>This is normally set for you based on whether the fragment is
248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * associated with a container view ID passed to
249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link FragmentTransaction#add(int, Fragment) FragmentTransaction.add(int, Fragment)}.
250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * If the fragment was added with a container, setShowsDialog will be
251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * initialized to false; otherwise, it will be true.
252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param showsDialog If true, the fragment will be displayed in a Dialog.
254c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas     * If false, no Dialog will be created and the fragment's view hierarchy
255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * left undisturbed.
256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setShowsDialog(boolean showsDialog) {
258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mShowsDialog = showsDialog;
259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Return the current value of {@link #setShowsDialog(boolean)}.
263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean getShowsDialog() {
265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mShowsDialog;
266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
26905c8a189672f5d883555e068549c0a84eeeff4aaAdam Powell    public void onAttach(Context context) {
27005c8a189672f5d883555e068549c0a84eeeff4aaAdam Powell        super.onAttach(context);
271ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        if (!mShownByMe) {
272ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            // If not explicitly shown through our API, take this as an
273ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            // indication that the dialog is no longer dismissed.
274ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            mDismissed = false;
275ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        }
276ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    }
277ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell
278ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    @Override
279ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    public void onDetach() {
280ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        super.onDetach();
281ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        if (!mShownByMe && !mDismissed) {
282ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            // The fragment was not shown by a direct call here, it is not
283ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            // dismissed, and now it is being detached...  well, okay, thou
284ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            // art now dismissed.  Have fun.
285ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            mDismissed = true;
286ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        }
287ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    }
288ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell
289ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell    @Override
29091d6ad872f1dab57871e92dd097e35c1e59aeb26Scott Kennedy    public void onCreate(@Nullable Bundle savedInstanceState) {
291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super.onCreate(savedInstanceState);
292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mShowsDialog = mContainerId == 0;
294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (savedInstanceState != null) {
296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mStyle = savedInstanceState.getInt(SAVED_STYLE, STYLE_NORMAL);
297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mTheme = savedInstanceState.getInt(SAVED_THEME, 0);
298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mCancelable = savedInstanceState.getBoolean(SAVED_CANCELABLE, true);
299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mShowsDialog = savedInstanceState.getBoolean(SAVED_SHOWS_DIALOG, mShowsDialog);
300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mBackStackId = savedInstanceState.getInt(SAVED_BACK_STACK_ID, -1);
301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
3050abeed582ee0a105edb41181d76e8ee034750f22George Mount    public LayoutInflater onGetLayoutInflater(Bundle savedInstanceState) {
306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!mShowsDialog) {
3070abeed582ee0a105edb41181d76e8ee034750f22George Mount            return super.onGetLayoutInflater(savedInstanceState);
308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mDialog = onCreateDialog(savedInstanceState);
311172a94c580e111987830a352c50d76c7db504088Chris Banes
312ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        if (mDialog != null) {
313172a94c580e111987830a352c50d76c7db504088Chris Banes            setupDialog(mDialog, mStyle);
314172a94c580e111987830a352c50d76c7db504088Chris Banes
315ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            return (LayoutInflater) mDialog.getContext().getSystemService(
316ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell                    Context.LAYOUT_INFLATER_SERVICE);
317ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        }
318d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        return (LayoutInflater) mHost.getContext().getSystemService(
319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Context.LAYOUT_INFLATER_SERVICE);
320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
321172a94c580e111987830a352c50d76c7db504088Chris Banes
322172a94c580e111987830a352c50d76c7db504088Chris Banes    /** @hide */
3238e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas    @RestrictTo(LIBRARY_GROUP)
324172a94c580e111987830a352c50d76c7db504088Chris Banes    public void setupDialog(Dialog dialog, int style) {
325172a94c580e111987830a352c50d76c7db504088Chris Banes        switch (style) {
326172a94c580e111987830a352c50d76c7db504088Chris Banes            case STYLE_NO_INPUT:
327172a94c580e111987830a352c50d76c7db504088Chris Banes                dialog.getWindow().addFlags(
328172a94c580e111987830a352c50d76c7db504088Chris Banes                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
329172a94c580e111987830a352c50d76c7db504088Chris Banes                                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
330172a94c580e111987830a352c50d76c7db504088Chris Banes                // fall through...
331172a94c580e111987830a352c50d76c7db504088Chris Banes            case STYLE_NO_FRAME:
332172a94c580e111987830a352c50d76c7db504088Chris Banes            case STYLE_NO_TITLE:
333172a94c580e111987830a352c50d76c7db504088Chris Banes                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
334172a94c580e111987830a352c50d76c7db504088Chris Banes        }
335172a94c580e111987830a352c50d76c7db504088Chris Banes    }
3368e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas
337cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Override to build your own custom Dialog container.  This is typically
339cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * used to show an AlertDialog instead of a generic Dialog; when doing so,
340cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} does not need
341cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to be implemented since the AlertDialog takes care of its own content.
3428e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas     *
343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>This method will be called after {@link #onCreate(Bundle)} and
344cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * before {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.  The
345cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * default implementation simply instantiates and returns a {@link Dialog}
346cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * class.
3478e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas     *
348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p><em>Note: DialogFragment own the {@link Dialog#setOnCancelListener
349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Dialog.setOnCancelListener} and {@link Dialog#setOnDismissListener
350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Dialog.setOnDismissListener} callbacks.  You must not set them yourself.</em>
351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * To find out about these events, override {@link #onCancel(DialogInterface)}
352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * and {@link #onDismiss(DialogInterface)}.</p>
3538e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas     *
354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param savedInstanceState The last saved instance state of the Fragment,
355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * or null if this is a freshly created Fragment.
3568e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas     *
357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return Return a new Dialog instance to be displayed by the Fragment.
358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
359a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    @NonNull
360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Dialog onCreateDialog(Bundle savedInstanceState) {
361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return new Dialog(getActivity(), getTheme());
362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
36490ac236504c1a4cac7e91f1ffc523334f2a8f399Aurimas Liutikas    @Override
365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onCancel(DialogInterface dialog) {
366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
36890ac236504c1a4cac7e91f1ffc523334f2a8f399Aurimas Liutikas    @Override
369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onDismiss(DialogInterface dialog) {
370ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell        if (!mViewDestroyed) {
371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // Note: we need to use allowStateLoss, because the dialog
372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // dispatches this asynchronously so we can receive the call
373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // after the activity is paused.  Worst case, when the user comes
374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // back to the activity they see the dialog again.
375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            dismissInternal(true);
376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onActivityCreated(Bundle savedInstanceState) {
381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super.onActivityCreated(savedInstanceState);
382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!mShowsDialog) {
384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return;
385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        View view = getView();
388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (view != null) {
389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (view.getParent() != null) {
39005c8a189672f5d883555e068549c0a84eeeff4aaAdam Powell                throw new IllegalStateException(
39105c8a189672f5d883555e068549c0a84eeeff4aaAdam Powell                        "DialogFragment can not be attached to a container view");
392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDialog.setContentView(view);
394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
39505c8a189672f5d883555e068549c0a84eeeff4aaAdam Powell        final Activity activity = getActivity();
39605c8a189672f5d883555e068549c0a84eeeff4aaAdam Powell        if (activity != null) {
39705c8a189672f5d883555e068549c0a84eeeff4aaAdam Powell            mDialog.setOwnerActivity(activity);
39805c8a189672f5d883555e068549c0a84eeeff4aaAdam Powell        }
399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mDialog.setCancelable(mCancelable);
400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mDialog.setOnCancelListener(this);
401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mDialog.setOnDismissListener(this);
402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (savedInstanceState != null) {
403cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            Bundle dialogState = savedInstanceState.getBundle(SAVED_DIALOG_STATE_TAG);
404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (dialogState != null) {
405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mDialog.onRestoreInstanceState(dialogState);
406cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
407cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
408cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
410cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
411cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onStart() {
412cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super.onStart();
41305c8a189672f5d883555e068549c0a84eeeff4aaAdam Powell
414cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDialog != null) {
415ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            mViewDestroyed = false;
416cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDialog.show();
417cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
418cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
419cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
420cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
421cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onSaveInstanceState(Bundle outState) {
422cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super.onSaveInstanceState(outState);
423cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDialog != null) {
424cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            Bundle dialogState = mDialog.onSaveInstanceState();
425cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (dialogState != null) {
426cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                outState.putBundle(SAVED_DIALOG_STATE_TAG, dialogState);
427cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
428cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
429cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mStyle != STYLE_NORMAL) {
430cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            outState.putInt(SAVED_STYLE, mStyle);
431cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
432cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mTheme != 0) {
433cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            outState.putInt(SAVED_THEME, mTheme);
434cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
435cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!mCancelable) {
436cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            outState.putBoolean(SAVED_CANCELABLE, mCancelable);
437cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
438cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!mShowsDialog) {
439cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            outState.putBoolean(SAVED_SHOWS_DIALOG, mShowsDialog);
440cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
441cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBackStackId != -1) {
442cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            outState.putInt(SAVED_BACK_STACK_ID, mBackStackId);
443cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
444cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
445cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
446cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
447cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onStop() {
448cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super.onStop();
449cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDialog != null) {
450cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDialog.hide();
451cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
452cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
453cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
454cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
455cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Remove dialog.
456cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
457cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
458cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onDestroyView() {
459cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super.onDestroyView();
460cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDialog != null) {
461cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // Set removed here because this dismissal is just to hide
462cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // the dialog -- we don't want this to cause the fragment to
463cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // actually be removed.
464ee8bdae679a94a9be65204b96d9352c4afb58a93Adam Powell            mViewDestroyed = true;
465cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDialog.dismiss();
466cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDialog = null;
467cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
468cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
469cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
470