Implementations should override this class and implement
+ * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} to supply the
+ * content of the dialog. Alternatively, they can override
+ * {@link #onCreateDialog(Bundle)} to create an entirely custom dialog, such
+ * as an AlertDialog, with its own content.
+ */
+public class DialogFragment extends Fragment
+ implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
+
+ /**
+ * Style for {@link #DialogFragment(int, int)} constructor: a basic,
+ * normal dialog.
+ */
+ public static final int STYLE_NORMAL = 0;
+
+ /**
+ * Style for {@link #DialogFragment(int, int)} constructor: don't include
+ * a title area.
+ */
+ public static final int STYLE_NO_TITLE = 1;
+
+ /**
+ * Style for {@link #DialogFragment(int, int)} constructor: don't draw
+ * any frame at all; the view hierarchy returned by {@link #onCreateView}
+ * is entirely responsible for drawing the dialog.
+ */
+ public static final int STYLE_NO_FRAME = 2;
+
+ /**
+ * Style for {@link #DialogFragment(int, int)} constructor: like
+ * {@link #STYLE_NO_FRAME}, but also disables all input to the dialog.
+ * The user can not touch it, and its window will not receive input focus.
+ */
+ public static final int STYLE_NO_INPUT = 3;
+
+ private static final String SAVED_DIALOG_STATE_TAG = "android:savedDialogState";
+ private static final String SAVED_STYLE = "android:style";
+ private static final String SAVED_THEME = "android:theme";
+ private static final String SAVED_CANCELABLE = "android:cancelable";
+ private static final String SAVED_BACK_STACK_ID = "android:backStackId";
+
+ int mStyle = STYLE_NORMAL;
+ int mTheme = 0;
+ boolean mCancelable = true;
+ int mBackStackId = -1;
+
+ Dialog mDialog;
+ boolean mDestroyed;
+
+ public DialogFragment() {
+ }
+
+ /**
+ * Constructor to customize the basic appearance and behavior of the
+ * fragment's dialog. This can be used for some common dialog behaviors,
+ * taking care of selecting flags, theme, and other options for you. The
+ * same effect can be achieve by manually setting Dialog and Window
+ * attributes yourself.
+ *
+ * @param style Selects a standard style: may be {@link #STYLE_NORMAL},
+ * {@link #STYLE_NO_TITLE}, {@link #STYLE_NO_FRAME}, or
+ * {@link #STYLE_NO_INPUT}.
+ * @param theme Optional custom theme. If 0, an appropriate theme (based
+ * on the style) will be selected for you.
+ */
+ public DialogFragment(int style, int theme) {
+ mStyle = style;
+ if (mStyle == STYLE_NO_FRAME || mStyle == STYLE_NO_INPUT) {
+ mTheme = android.R.style.Theme_Dialog_NoFrame;
+ }
+ if (theme != 0) {
+ mTheme = theme;
+ }
+ }
+
+ /**
+ * Display the dialog, adding the fragment to the given activity. This
+ * is a convenience for explicitly creating a transaction, adding the
+ * fragment to it with the given tag, and committing it. This does
+ * not add the transaction to the back stack. When the fragment
+ * is dismissed, a new transaction will be executed to remove it from
+ * the activity.
+ * @param activity The activity this fragment will be added to.
+ * @param tag The tag for this fragment, as per
+ * {@link FragmentTransaction#add(Fragment, String) FragmentTransaction.add}.
+ */
+ public void show(Activity activity, String tag) {
+ FragmentTransaction ft = activity.openFragmentTransaction();
+ ft.add(this, tag);
+ ft.commit();
+ }
+
+ /**
+ * Display the dialog, adding the fragment to the given activity using
+ * an existing transaction and then committing the transaction.
+ * @param activity The activity this fragment will be added to.
+ * @param transaction An existing transaction in which to add the fragment.
+ * @param tag The tag for this fragment, as per
+ * {@link FragmentTransaction#add(Fragment, String) FragmentTransaction.add}.
+ * @return Returns the identifier of the committed transaction, as per
+ * {@link FragmentTransaction#commit() FragmentTransaction.commit()}.
+ */
+ public int show(Activity activity, FragmentTransaction transaction, String tag) {
+ transaction.add(this, tag);
+ mBackStackId = transaction.commit();
+ return mBackStackId;
+ }
+
+ /**
+ * Dismiss the fragment and its dialog. If the fragment was added to the
+ * back stack, all back stack state up to and including this entry will
+ * be popped. Otherwise, a new transaction will be committed to remove
+ * the fragment.
+ */
+ public void dismiss() {
+ if (mDialog != null) {
+ mDialog.dismiss();
+ }
+ if (mBackStackId >= 0) {
+ getActivity().popBackStack(mBackStackId, Activity.POP_BACK_STACK_INCLUSIVE);
+ mBackStackId = -1;
+ } else {
+ FragmentTransaction ft = getActivity().openFragmentTransaction();
+ ft.remove(this);
+ ft.commit();
+ }
+ }
+
+ public Dialog getDialog() {
+ return mDialog;
+ }
+
+ public int getTheme() {
+ return mTheme;
+ }
+
+ public void setCancelable(boolean cancelable) {
+ mCancelable = cancelable;
+ if (mDialog != null) mDialog.setCancelable(cancelable);
+ }
+
+ public boolean getCancelable() {
+ return mCancelable;
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ if (savedInstanceState != null) {
+ mStyle = savedInstanceState.getInt(SAVED_STYLE, mStyle);
+ mTheme = savedInstanceState.getInt(SAVED_THEME, mTheme);
+ mCancelable = savedInstanceState.getBoolean(SAVED_CANCELABLE, mCancelable);
+ mBackStackId = savedInstanceState.getInt(SAVED_BACK_STACK_ID, mBackStackId);
+ }
+ }
+
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ return new Dialog(getActivity(), getTheme());
+ }
+
+ public void onCancel(DialogInterface dialog) {
+ if (mBackStackId >= 0) {
+ // If this fragment is part of the back stack, then cancelling
+ // the dialog means popping off the back stack.
+ getActivity().popBackStack(mBackStackId, Activity.POP_BACK_STACK_INCLUSIVE);
+ mBackStackId = -1;
+ }
+ }
+
+ public void onDismiss(DialogInterface dialog) {
+ }
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+ mDialog = onCreateDialog(savedInstanceState);
+ mDestroyed = false;
+ switch (mStyle) {
+ case STYLE_NO_INPUT:
+ mDialog.getWindow().addFlags(
+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
+ WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
+ // fall through...
+ case STYLE_NO_FRAME:
+ case STYLE_NO_TITLE:
+ mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
+ }
+ View view = getView();
+ if (view != null) {
+ if (view.getParent() != null) {
+ throw new IllegalStateException("DialogFragment can not be attached to a container view");
+ }
+ mDialog.setContentView(view);
+ }
+ mDialog.setOwnerActivity(getActivity());
+ mDialog.setCancelable(mCancelable);
+ mDialog.setOnCancelListener(this);
+ mDialog.setOnDismissListener(this);
+ if (savedInstanceState != null) {
+ Bundle dialogState = savedInstanceState.getBundle(SAVED_DIALOG_STATE_TAG);
+ if (dialogState != null) {
+ mDialog.onRestoreInstanceState(dialogState);
+ }
+ }
+ }
+
+ @Override
+ public void onStart() {
+ super.onStart();
+ mDialog.show();
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ if (mDialog != null) {
+ Bundle dialogState = mDialog.onSaveInstanceState();
+ if (dialogState != null) {
+ outState.putBundle(SAVED_DIALOG_STATE_TAG, dialogState);
+ }
+ }
+ outState.putInt(SAVED_STYLE, mStyle);
+ outState.putInt(SAVED_THEME, mTheme);
+ outState.putBoolean(SAVED_CANCELABLE, mCancelable);
+ outState.putInt(SAVED_BACK_STACK_ID, mBackStackId);
+ }
+
+ @Override
+ public void onStop() {
+ super.onStop();
+ mDialog.hide();
+ }
+
+ /**
+ * Detach from list view.
+ */
+ @Override
+ public void onDestroyView() {
+ super.onDestroyView();
+ mDestroyed = true;
+ mDialog.dismiss();
+ mDialog = null;
+ }
+}
diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java
index 4f3043c40c467..c0e757d1ab0b7 100644
--- a/core/java/android/app/FragmentManager.java
+++ b/core/java/android/app/FragmentManager.java
@@ -86,6 +86,10 @@ public class FragmentManager {
ArrayList
- * ListActivity hosts a {@link android.widget.ListView ListView} object that can
+ * ListFragment hosts a {@link android.widget.ListView ListView} object that can
* be bound to different data sources, typically either an array or a Cursor
* holding query results. Binding, screen layout, and row layout are discussed
* in the following sections.
@@ -39,10 +39,10 @@ import android.widget.TextView;
* Screen Layout
*
- * ListActivity has a default layout that consists of a single list view.
+ * ListFragment has a default layout that consists of a single list view.
* However, if you desire, you can customize the fragment layout by returning
* your own view hierarchy from {@link #onCreateView}.
- * To do this, your view hierarchy MUST contain a ListView object with the
+ * To do this, your view hierarchy must contain a ListView object with the
* id "@android:id/list" (or {@link android.R.id#list} if it's in code)
*
* Optionally, your view hierarchy can contain another view object of any type to
@@ -50,7 +50,7 @@ import android.widget.TextView;
* id "android:empty". Note that when an empty view is present, the list view
* will be hidden when there is no data to display.
*
- * The following code demonstrates an (ugly) custom lisy layout. It has a list
+ * The following code demonstrates an (ugly) custom list layout. It has a list
* with a green background, and an alternate red "no data" message.
*
+ * You must use + * {@link #setListAdapter(ListAdapter) ListFragment.setListAdapter()} to + * associate the list with an adapter. Do not directly call + * {@link ListView#setAdapter(ListAdapter) ListView.setAdapter()} or else + * important initialization will be skipped. + *
* * @see #setListAdapter * @see android.widget.ListView @@ -160,6 +167,7 @@ public class ListFragment extends Fragment { TextView mStandardEmptyView; View mProgressContainer; View mListContainer; + boolean mSetEmptyText; boolean mListShown; public ListFragment() { @@ -280,7 +288,11 @@ public class ListFragment extends Fragment { if (mStandardEmptyView == null) { throw new IllegalStateException("Can't be used with a custom content view"); } - mList.setEmptyView(mStandardEmptyView); + mStandardEmptyView.setText(text); + if (!mSetEmptyText) { + mList.setEmptyView(mStandardEmptyView); + mSetEmptyText = true; + } } /** diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml index d0be554efeb02..34dd46c57f4dc 100644 --- a/core/res/res/values/public.xml +++ b/core/res/res/values/public.xml @@ -1314,6 +1314,7 @@