diff options
author | Dianne Hackborn <hackbod@google.com> | 2011-03-03 21:48:24 -0800 |
---|---|---|
committer | Dianne Hackborn <hackbod@google.com> | 2011-03-03 22:17:16 -0800 |
commit | e3a7f628c6d9fef42be24999b3137ebe5c6f3525 (patch) | |
tree | 48ea9dc2b28e2d27c4da5c104491b31d087d2192 /core | |
parent | 6c8d76527c938eb49182a6fd0677ccbc1618e7a8 (diff) | |
download | frameworks_base-e3a7f628c6d9fef42be24999b3137ebe5c6f3525.zip frameworks_base-e3a7f628c6d9fef42be24999b3137ebe5c6f3525.tar.gz frameworks_base-e3a7f628c6d9fef42be24999b3137ebe5c6f3525.tar.bz2 |
Fix Fragment.onInflate() to actually work correctly.
Like, um, it needs to be given the Activity since this is called before
the activity is attached.
And it was called after the entire fragment and its *view* was created
when being restored from saved state.
And the documentation was whacked.
Also fix the IME selector to dismiss when you tap outside of it.
Change-Id: Icbcafe7558965a570bdef9cda3441b1f0f7a317c
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/app/Activity.java | 4 | ||||
-rw-r--r-- | core/java/android/app/Fragment.java | 53 | ||||
-rw-r--r-- | core/java/android/app/FragmentManager.java | 6 | ||||
-rw-r--r-- | core/java/android/os/Bundle.java | 45 |
4 files changed, 91 insertions, 17 deletions
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index 3a82c78..edfd6ef 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -4137,7 +4137,7 @@ public class Activity extends ContextThemeWrapper fragment.mInLayout = true; fragment.mImmediateActivity = this; fragment.mFragmentManager = mFragments; - fragment.onInflate(attrs, fragment.mSavedFragmentState); + fragment.onInflate(this, attrs, fragment.mSavedFragmentState); mFragments.addFragment(fragment, true); } else if (fragment.mInLayout) { @@ -4156,7 +4156,7 @@ public class Activity extends ContextThemeWrapper // from last saved state), then give it the attributes to // initialize itself. if (!fragment.mRetaining) { - fragment.onInflate(attrs, fragment.mSavedFragmentState); + fragment.onInflate(this, attrs, fragment.mSavedFragmentState); } mFragments.moveToState(fragment); } diff --git a/core/java/android/app/Fragment.java b/core/java/android/app/Fragment.java index 8982110f..53dc7c8 100644 --- a/core/java/android/app/Fragment.java +++ b/core/java/android/app/Fragment.java @@ -859,32 +859,57 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener } /** + * @deprecated Use {@link #onInflate(Activity, AttributeSet, Bundle)} instead. + */ + @Deprecated + public void onInflate(AttributeSet attrs, Bundle savedInstanceState) { + mCalled = true; + } + + /** * Called when a fragment is being created as part of a view layout * inflation, typically from setting the content view of an activity. This - * will be called immediately after the fragment is created from a <fragment> + * may be called immediately after the fragment is created from a <fragment> * tag in a layout file. Note this is <em>before</em> the fragment's * {@link #onAttach(Activity)} has been called; all you should do here is - * parse the attributes and save them away. A convenient thing to do is - * simply copy them into a Bundle that is given to {@link #setArguments(Bundle)}. + * parse the attributes and save them away. * * <p>This is called every time the fragment is inflated, even if it is - * being inflated into a new instance with saved state. Because a fragment's - * arguments are retained across instances, it may make no sense to re-parse - * the attributes into new arguments. You may want to first check - * {@link #getArguments()} and only parse the attributes if it returns null, - * the assumption being that if it is non-null those are the same arguments - * from the first time the fragment was inflated. (That said, you may want - * to have layouts change for different configurations such as landscape - * and portrait, which can have different attributes. If so, you will need - * to re-parse the attributes each time this is called to generate new - * arguments.)</p> + * being inflated into a new instance with saved state. It typically makes + * sense to re-parse the parameters each time, to allow them to change with + * different configurations.</p> + * + * <p>Here is a typical implementation of a fragment that can take parameters + * both through attributes supplied here as well from {@link #getArguments()}:</p> + * + * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java + * fragment} + * + * <p>Note that parsing the XML attributes uses a "styleable" resource. The + * declaration for the styleable used here is:</p> + * + * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments} * + * <p>The fragment can then be declared within its activity's content layout + * through a tag like this:</p> + * + * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes} + * + * <p>This fragment can also be created dynamically from arguments given + * at runtime in the arguments Bundle; here is an example of doing so at + * creation of the containing activity:</p> + * + * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java + * create} + * + * @param activity The Activity that is inflating this fragment. * @param attrs The attributes at the tag where the fragment is * being created. * @param savedInstanceState If the fragment is being re-created from * a previous saved state, this is the state. */ - public void onInflate(AttributeSet attrs, Bundle savedInstanceState) { + public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { + onInflate(attrs, savedInstanceState); mCalled = true; } diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java index d8d0a5b..ab60cf0 100644 --- a/core/java/android/app/FragmentManager.java +++ b/core/java/android/app/FragmentManager.java @@ -660,6 +660,12 @@ final class FragmentManagerImpl extends FragmentManager { } if (f.mState < newState) { + // For fragments that are created from a layout, when restoring from + // state we don't want to allow them to be created until they are + // being reloaded from the layout. + if (f.mFromLayout && !f.mInLayout) { + return; + } if (f.mAnimatingAway != null) { // The fragment is currently being animated... but! Now we // want to move our state back up. Give up on waiting for the diff --git a/core/java/android/os/Bundle.java b/core/java/android/os/Bundle.java index 8eac7aa..c288f8a 100644 --- a/core/java/android/os/Bundle.java +++ b/core/java/android/os/Bundle.java @@ -1028,7 +1028,6 @@ public final class Bundle implements Parcelable, Cloneable { } } - /** * Returns the value associated with the given key, or null if * no mapping of the desired type exists for the given key or a null @@ -1052,6 +1051,28 @@ public final class Bundle implements Parcelable, Cloneable { } /** + * Returns the value associated with the given key, or defaultValue if + * no mapping of the desired type exists for the given key. + * + * @param key a String, or null + * @param defaultValue Value to return if key does not exist + * @return a String value, or null + */ + public String getString(String key, String defaultValue) { + unparcel(); + Object o = mMap.get(key); + if (o == null) { + return defaultValue; + } + try { + return (String) o; + } catch (ClassCastException e) { + typeWarning(key, o, "String", e); + return defaultValue; + } + } + + /** * Returns the value associated with the given key, or null if * no mapping of the desired type exists for the given key or a null * value is explicitly associated with the key. @@ -1074,6 +1095,28 @@ public final class Bundle implements Parcelable, Cloneable { } /** + * Returns the value associated with the given key, or defaultValue if + * no mapping of the desired type exists for the given key. + * + * @param key a String, or null + * @param defaultValue Value to return if key does not exist + * @return a CharSequence value, or null + */ + public CharSequence getCharSequence(String key, CharSequence defaultValue) { + unparcel(); + Object o = mMap.get(key); + if (o == null) { + return defaultValue; + } + try { + return (CharSequence) o; + } catch (ClassCastException e) { + typeWarning(key, o, "CharSequence", e); + return defaultValue; + } + } + + /** * Returns the value associated with the given key, or null if * no mapping of the desired type exists for the given key or a null * value is explicitly associated with the key. |