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