From b31e84bc4513e46bac4be8f8d0513f78e360fb11 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Tue, 8 Jun 2010 18:04:35 -0700 Subject: [PATCH] Add Fragment option menu APIs. Also fix up how transactions are handled so that a series of transactions can correctly be created and committed. Change-Id: I948ba47d49e9b2246a1958bd9eac9dd36dc5a855 --- api/current.xml | 105 ++++++++++++++++-- core/java/android/app/Activity.java | 24 +++- core/java/android/app/BackStackEntry.java | 96 ++++++++++++---- core/java/android/app/Fragment.java | 105 ++++++++++++++++-- core/java/android/app/FragmentManager.java | 71 ++++++++++++ core/java/android/view/Window.java | 2 + .../internal/policy/impl/PhoneWindow.java | 21 +++- 7 files changed, 381 insertions(+), 43 deletions(-) diff --git a/api/current.xml b/api/current.xml index 0ffbb1b66107c..287b3f327763a 100644 --- a/api/current.xml +++ b/api/current.xml @@ -20306,6 +20306,17 @@ visibility="public" > + + @@ -25554,7 +25565,7 @@ native="false" synchronized="false" static="false" - final="false" + final="true" deprecated="not deprecated" visibility="public" > @@ -25565,7 +25576,7 @@ native="false" synchronized="false" static="false" - final="false" + final="true" deprecated="not deprecated" visibility="public" > @@ -25576,7 +25587,7 @@ native="false" synchronized="false" static="false" - final="false" + final="true" deprecated="not deprecated" visibility="public" > @@ -25609,7 +25620,7 @@ native="false" synchronized="false" static="false" - final="false" + final="true" deprecated="not deprecated" visibility="public" > @@ -25620,7 +25631,7 @@ native="false" synchronized="false" static="false" - final="false" + final="true" deprecated="not deprecated" visibility="public" > @@ -25631,7 +25642,7 @@ native="false" synchronized="false" static="false" - final="false" + final="true" deprecated="not deprecated" visibility="public" > @@ -25709,6 +25720,21 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + menu. @@ -3887,6 +3900,7 @@ public class Activity extends ContextThemeWrapper fragment.mFromLayout = true; fragment.mFragmentId = id; fragment.mTag = tag; + fragment.mImmediateActivity = this; mFragments.addFragment(fragment, true); } // If this fragment is newly instantiated (either right now, or diff --git a/core/java/android/app/BackStackEntry.java b/core/java/android/app/BackStackEntry.java index 33e456d1f5f21..5e9aea5362a3a 100644 --- a/core/java/android/app/BackStackEntry.java +++ b/core/java/android/app/BackStackEntry.java @@ -100,9 +100,10 @@ final class BackStackEntry implements FragmentTransaction, Runnable { static final int OP_NULL = 0; static final int OP_ADD = 1; - static final int OP_REMOVE = 2; - static final int OP_HIDE = 3; - static final int OP_SHOW = 4; + static final int OP_REPLACE = 2; + static final int OP_REMOVE = 3; + static final int OP_HIDE = 4; + static final int OP_SHOW = 5; static final class Op { Op next; @@ -111,6 +112,7 @@ final class BackStackEntry implements FragmentTransaction, Runnable { Fragment fragment; int enterAnim; int exitAnim; + ArrayList removed; } Op mHead; @@ -142,17 +144,25 @@ final class BackStackEntry implements FragmentTransaction, Runnable { } public FragmentTransaction add(Fragment fragment, String tag) { - return add(0, fragment, tag); + doAddOp(0, fragment, tag, OP_ADD); + return this; } public FragmentTransaction add(int containerViewId, Fragment fragment) { - return add(containerViewId, fragment, null); + doAddOp(containerViewId, fragment, null, OP_ADD); + return this; } public FragmentTransaction add(int containerViewId, Fragment fragment, String tag) { - if (fragment.mActivity != null) { + doAddOp(containerViewId, fragment, tag, OP_ADD); + return this; + } + + private void doAddOp(int containerViewId, Fragment fragment, String tag, int opcmd) { + if (fragment.mImmediateActivity != null) { throw new IllegalStateException("Fragment already added: " + fragment); } + fragment.mImmediateActivity = mManager.mActivity; if (tag != null) { if (fragment.mTag != null && !tag.equals(fragment.mTag)) { @@ -173,11 +183,9 @@ final class BackStackEntry implements FragmentTransaction, Runnable { } Op op = new Op(); - op.cmd = OP_ADD; + op.cmd = opcmd; op.fragment = fragment; addOp(op); - - return this; } public FragmentTransaction replace(int containerViewId, Fragment fragment) { @@ -188,21 +196,16 @@ final class BackStackEntry implements FragmentTransaction, Runnable { if (containerViewId == 0) { throw new IllegalArgumentException("Must use non-zero containerViewId"); } - if (mManager.mAdded != null) { - for (int i=0; i(); + } + op.removed.add(old); + if (mAddToBackStack) { + old.mBackStackNesting++; + } + old.mNextAnim = op.exitAnim; + mManager.removeFragment(old, mTransition, mTransitionStyle); + } + } + } + if (mAddToBackStack) { + f.mBackStackNesting++; + } + f.mNextAnim = op.enterAnim; + mManager.addFragment(f, false); + } break; case OP_REMOVE: { Fragment f = op.fragment; if (mAddToBackStack) { @@ -312,6 +339,11 @@ final class BackStackEntry implements FragmentTransaction, Runnable { mManager.moveToState(mManager.mCurState, mTransition, mTransitionStyle, true); + if (mManager.mNeedMenuInvalidate && mManager.mActivity != null) { + mManager.mActivity.invalidateOptionsMenu(); + mManager.mNeedMenuInvalidate = false; + } + if (mAddToBackStack) { mManager.addBackStackState(this); } @@ -330,6 +362,24 @@ final class BackStackEntry implements FragmentTransaction, Runnable { FragmentManager.reverseTransit(mTransition), mTransitionStyle); } break; + case OP_REPLACE: { + Fragment f = op.fragment; + if (mAddToBackStack) { + f.mBackStackNesting--; + } + mManager.removeFragment(f, + FragmentManager.reverseTransit(mTransition), + mTransitionStyle); + if (op.removed != null) { + for (int i=0; imenu. For this method + * to be called, you must have first called {@link #setHasMenu}. See + * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu} + * for more information. + * + * @param menu The options menu in which you place your items. + * + * @see #setHasMenu + * @see #onPrepareOptionsMenu + * @see #onOptionsItemSelected + */ + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + } + + /** + * Prepare the Screen's standard options menu to be displayed. This is + * called right before the menu is shown, every time it is shown. You can + * use this method to efficiently enable/disable items or otherwise + * dynamically modify the contents. See + * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu} + * for more information. + * + * @param menu The options menu as last shown or first initialized by + * onCreateOptionsMenu(). + * + * @see #setHasMenu + * @see #onCreateOptionsMenu + */ + public void onPrepareOptionsMenu(Menu menu) { + } + + /** + * This hook is called whenever an item in your options menu is selected. + * The default implementation simply returns false to have the normal + * processing happen (calling the item's Runnable or sending a message to + * its Handler as appropriate). You can use this method for any items + * for which you would like to do processing without those other + * facilities. + * + *

Derived classes should call through to the base class for it to + * perform the default menu handling. + * + * @param item The menu item that was selected. + * + * @return boolean Return false to allow normal menu processing to + * proceed, true to consume it here. + * + * @see #onCreateOptionsMenu + */ + public boolean onOptionsItemSelected(MenuItem item) { + return false; + } + + /** + * This hook is called whenever the options menu is being closed (either by the user canceling + * the menu with the back/menu button, or when an item is selected). + * + * @param menu The options menu as last shown or first initialized by + * onCreateOptionsMenu(). + */ + public void onOptionsMenuClosed(Menu menu) { + } } diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java index a10a19189d669..b837c323483f3 100644 --- a/core/java/android/app/FragmentManager.java +++ b/core/java/android/app/FragmentManager.java @@ -23,6 +23,9 @@ import android.os.Parcel; import android.os.Parcelable; import android.util.Log; import android.util.SparseArray; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; @@ -82,6 +85,8 @@ public class FragmentManager { int mCurState = Fragment.INITIALIZING; Activity mActivity; + boolean mNeedMenuInvalidate; + // Temporary vars for state save and restore. Bundle mStateBundle = null; SparseArray mStateArray = null; @@ -362,6 +367,9 @@ public class FragmentManager { mAdded.add(fragment); makeActive(fragment); fragment.mAdded = true; + if (fragment.mHasMenu) { + mNeedMenuInvalidate = true; + } if (moveToStateNow) { moveToState(fragment, mCurState, 0, 0); } @@ -374,6 +382,9 @@ public class FragmentManager { if (inactive) { makeInactive(fragment); } + if (fragment.mHasMenu) { + mNeedMenuInvalidate = true; + } fragment.mAdded = false; moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED, transition, transitionStyle); @@ -391,6 +402,9 @@ public class FragmentManager { } fragment.mView.setVisibility(View.GONE); } + if (fragment.mAdded && fragment.mHasMenu) { + mNeedMenuInvalidate = true; + } fragment.onHiddenChanged(true); } } @@ -407,6 +421,9 @@ public class FragmentManager { } fragment.mView.setVisibility(View.VISIBLE); } + if (fragment.mAdded && fragment.mHasMenu) { + mNeedMenuInvalidate = true; + } fragment.onHiddenChanged(false); } } @@ -673,6 +690,7 @@ public class FragmentManager { "No instantiated fragment for index #" + fms.mAdded[i]); } f.mAdded = true; + f.mImmediateActivity = mActivity; mAdded.add(f); } } else { @@ -721,6 +739,59 @@ public class FragmentManager { mActivity = null; } + public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { + boolean show = false; + if (mActive != null) { + for (int i=0; i