FragmentActivity.java revision e0f27d39b0a4f0ef30ef6446e7b675279961cc94
1affc150dc44fab1911775a49636d0ce85333b634Zonr Chang/* 2affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * Copyright (C) 2011 The Android Open Source Project 3affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * 4affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * Licensed under the Apache License, Version 2.0 (the "License"); 5affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * you may not use this file except in compliance with the License. 6affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * You may obtain a copy of the License at 7affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * 8affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * http://www.apache.org/licenses/LICENSE-2.0 9affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * 10affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * Unless required by applicable law or agreed to in writing, software 11affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * distributed under the License is distributed on an "AS IS" BASIS, 12affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * See the License for the specific language governing permissions and 14affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * limitations under the License. 15affc150dc44fab1911775a49636d0ce85333b634Zonr Chang */ 16affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 17affc150dc44fab1911775a49636d0ce85333b634Zonr Changpackage android.support.v4.app; 18affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 19affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.app.Activity; 20affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.content.Context; 21affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.content.Intent; 22affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.content.res.Configuration; 23affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.content.res.Resources; 24affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.content.res.TypedArray; 25affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.os.Bundle; 26affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.os.Handler; 27affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.os.Message; 28affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.os.Parcelable; 29affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.support.v4.view.MenuInflater; 30affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.util.AttributeSet; 31affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.util.Log; 32affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.view.KeyEvent; 33affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.view.Menu; 34affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.view.MenuItem; 35affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.view.View; 36affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.view.ViewGroup; 37affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport android.view.Window; 38affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 39affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport java.io.FileDescriptor; 40affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport java.io.PrintWriter; 41affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport java.util.ArrayList; 42affc150dc44fab1911775a49636d0ce85333b634Zonr Changimport java.util.HashMap; 43affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 44affc150dc44fab1911775a49636d0ce85333b634Zonr Chang/** 45affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * Base class for activities that want to use the support-based 46affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * {@link android.support.v4.app.Fragment} and 47affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * {@link android.support.v4.content.Loader} APIs. 48affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * 49affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * <p>When using this class as opposed to new platform's built-in fragment 50affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * and loader support, you must use the {@link #getSupportFragmentManager()} 51affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * and {@link #getSupportLoaderManager()} methods respectively to access 52affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * those features. 53affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * 54affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * <p>Known limitations:</p> 55affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * <ul> 56affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * <li> <p>When using the <fragment> tag, this implementation can not 57affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * use the parent view's ID as the new fragment's ID. You must explicitly 58affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * specify an ID (or tag) in the <fragment>.</p> 59affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * <li> <p>Prior to Honeycomb (3.0), an activity's state was saved before pausing. 60affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * Fragments are a significant amount of new state, and dynamic enough that one 61affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * often wants them to change between pausing and stopping. These classes 62affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * throw an exception if you try to change the fragment state after it has been 63affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * saved, to avoid accidental loss of UI state. However this is too restrictive 64affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * prior to Honeycomb, where the state is saved before pausing. To address this, 65affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * when running on platforms prior to Honeycomb an exception will not be thrown 66affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * if you change fragments between the state save and the activity being stopped. 67affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * This means that in some cases if the activity is restored from its last saved 68affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * state, this may be a snapshot slightly before what the user last saw.</p> 69affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * </ul> 70affc150dc44fab1911775a49636d0ce85333b634Zonr Chang */ 71affc150dc44fab1911775a49636d0ce85333b634Zonr Changpublic class FragmentActivity extends Activity { 72affc150dc44fab1911775a49636d0ce85333b634Zonr Chang private static final String TAG = "FragmentActivity"; 73affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 74affc150dc44fab1911775a49636d0ce85333b634Zonr Chang static final String FRAGMENTS_TAG = "android:support:fragments"; 75affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 76affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // This is the SDK API version of Honeycomb (3.0). 77affc150dc44fab1911775a49636d0ce85333b634Zonr Chang private static final int HONEYCOMB = 11; 78affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 79affc150dc44fab1911775a49636d0ce85333b634Zonr Chang static final int MSG_REALLY_STOPPED = 1; 80affc150dc44fab1911775a49636d0ce85333b634Zonr Chang static final int MSG_RESUME_PENDING = 2; 81affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 82affc150dc44fab1911775a49636d0ce85333b634Zonr Chang final Handler mHandler = new Handler() { 83affc150dc44fab1911775a49636d0ce85333b634Zonr Chang @Override 84affc150dc44fab1911775a49636d0ce85333b634Zonr Chang public void handleMessage(Message msg) { 85affc150dc44fab1911775a49636d0ce85333b634Zonr Chang switch (msg.what) { 86affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case MSG_REALLY_STOPPED: 87affc150dc44fab1911775a49636d0ce85333b634Zonr Chang if (mStopped) { 88affc150dc44fab1911775a49636d0ce85333b634Zonr Chang doReallyStop(false); 89affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 9022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao break; 9122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao case MSG_RESUME_PENDING: 92affc150dc44fab1911775a49636d0ce85333b634Zonr Chang onResumeFragments(); 93affc150dc44fab1911775a49636d0ce85333b634Zonr Chang mFragments.execPendingActions(); 94affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 95affc150dc44fab1911775a49636d0ce85333b634Zonr Chang default: 96affc150dc44fab1911775a49636d0ce85333b634Zonr Chang super.handleMessage(msg); 97affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 98affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 99affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 100affc150dc44fab1911775a49636d0ce85333b634Zonr Chang }; 101affc150dc44fab1911775a49636d0ce85333b634Zonr Chang final FragmentManagerImpl mFragments = new FragmentManagerImpl(); 102affc150dc44fab1911775a49636d0ce85333b634Zonr Chang final FragmentContainer mContainer = new FragmentContainer() { 103affc150dc44fab1911775a49636d0ce85333b634Zonr Chang @Override 104affc150dc44fab1911775a49636d0ce85333b634Zonr Chang public View findViewById(int id) { 105affc150dc44fab1911775a49636d0ce85333b634Zonr Chang return FragmentActivity.this.findViewById(id); 106affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 107affc150dc44fab1911775a49636d0ce85333b634Zonr Chang }; 108affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 109affc150dc44fab1911775a49636d0ce85333b634Zonr Chang boolean mCreated; 110affc150dc44fab1911775a49636d0ce85333b634Zonr Chang boolean mResumed; 111affc150dc44fab1911775a49636d0ce85333b634Zonr Chang boolean mStopped; 112affc150dc44fab1911775a49636d0ce85333b634Zonr Chang boolean mReallyStopped; 113affc150dc44fab1911775a49636d0ce85333b634Zonr Chang boolean mRetaining; 114affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 115affc150dc44fab1911775a49636d0ce85333b634Zonr Chang boolean mOptionsMenuInvalidated; 116affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 117affc150dc44fab1911775a49636d0ce85333b634Zonr Chang boolean mCheckedForLoaderManager; 118affc150dc44fab1911775a49636d0ce85333b634Zonr Chang boolean mLoadersStarted; 119affc150dc44fab1911775a49636d0ce85333b634Zonr Chang HashMap<String, LoaderManagerImpl> mAllLoaderManagers; 120affc150dc44fab1911775a49636d0ce85333b634Zonr Chang LoaderManagerImpl mLoaderManager; 121affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 122affc150dc44fab1911775a49636d0ce85333b634Zonr Chang MenuInflater mMenuInflater; 123affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 124affc150dc44fab1911775a49636d0ce85333b634Zonr Chang static final class NonConfigurationInstances { 125affc150dc44fab1911775a49636d0ce85333b634Zonr Chang Object activity; 126affc150dc44fab1911775a49636d0ce85333b634Zonr Chang Object custom; 127affc150dc44fab1911775a49636d0ce85333b634Zonr Chang HashMap<String, Object> children; 128affc150dc44fab1911775a49636d0ce85333b634Zonr Chang ArrayList<Fragment> fragments; 129affc150dc44fab1911775a49636d0ce85333b634Zonr Chang HashMap<String, LoaderManagerImpl> loaders; 130affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 131affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 132affc150dc44fab1911775a49636d0ce85333b634Zonr Chang static class FragmentTag { 133affc150dc44fab1911775a49636d0ce85333b634Zonr Chang public static final int[] Fragment = { 134affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 0x01010003, 0x010100d0, 0x010100d1 135affc150dc44fab1911775a49636d0ce85333b634Zonr Chang }; 136affc150dc44fab1911775a49636d0ce85333b634Zonr Chang public static final int Fragment_id = 1; 137affc150dc44fab1911775a49636d0ce85333b634Zonr Chang public static final int Fragment_name = 0; 138affc150dc44fab1911775a49636d0ce85333b634Zonr Chang public static final int Fragment_tag = 2; 139affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 140affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 141affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // ------------------------------------------------------------------------ 142affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // HOOKS INTO ACTIVITY 143affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // ------------------------------------------------------------------------ 144affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 145affc150dc44fab1911775a49636d0ce85333b634Zonr Chang /** 146affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * Dispatch incoming result to the correct fragment. 147affc150dc44fab1911775a49636d0ce85333b634Zonr Chang */ 148affc150dc44fab1911775a49636d0ce85333b634Zonr Chang @Override 149affc150dc44fab1911775a49636d0ce85333b634Zonr Chang protected void onActivityResult(int requestCode, int resultCode, Intent data) { 150affc150dc44fab1911775a49636d0ce85333b634Zonr Chang mFragments.noteStateNotSaved(); 151affc150dc44fab1911775a49636d0ce85333b634Zonr Chang int index = requestCode>>16; 152affc150dc44fab1911775a49636d0ce85333b634Zonr Chang if (index != 0) { 153affc150dc44fab1911775a49636d0ce85333b634Zonr Chang index--; 154affc150dc44fab1911775a49636d0ce85333b634Zonr Chang if (mFragments.mActive == null || index < 0 || index >= mFragments.mActive.size()) { 155affc150dc44fab1911775a49636d0ce85333b634Zonr Chang Log.w(TAG, "Activity result fragment index out of range: 0x" 156affc150dc44fab1911775a49636d0ce85333b634Zonr Chang + Integer.toHexString(requestCode)); 157affc150dc44fab1911775a49636d0ce85333b634Zonr Chang return; 158affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 159affc150dc44fab1911775a49636d0ce85333b634Zonr Chang Fragment frag = mFragments.mActive.get(index); 160f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines if (frag == null) { 161f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines Log.w(TAG, "Activity result no fragment exists for index: 0x" 162f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines + Integer.toHexString(requestCode)); 163f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines } else { 164f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines frag.onActivityResult(requestCode&0xffff, resultCode, data); 165affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 166affc150dc44fab1911775a49636d0ce85333b634Zonr Chang return; 167affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 168affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 169affc150dc44fab1911775a49636d0ce85333b634Zonr Chang super.onActivityResult(requestCode, resultCode, data); 170affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 171affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 172affc150dc44fab1911775a49636d0ce85333b634Zonr Chang /** 173affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * Take care of popping the fragment back stack or finishing the activity 174affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * as appropriate. 175affc150dc44fab1911775a49636d0ce85333b634Zonr Chang */ 176affc150dc44fab1911775a49636d0ce85333b634Zonr Chang public void onBackPressed() { 177 if (!mFragments.popBackStackImmediate()) { 178 finish(); 179 } 180 } 181 182 /** 183 * Dispatch configuration change to all fragments. 184 */ 185 @Override 186 public void onConfigurationChanged(Configuration newConfig) { 187 super.onConfigurationChanged(newConfig); 188 mFragments.dispatchConfigurationChanged(newConfig); 189 } 190 191 /** 192 * Perform initialization of all fragments and loaders. 193 */ 194 @Override 195 protected void onCreate(Bundle savedInstanceState) { 196 mFragments.attachActivity(this, mContainer, null); 197 // Old versions of the platform didn't do this! 198 if (getLayoutInflater().getFactory() == null) { 199 getLayoutInflater().setFactory(this); 200 } 201 202 super.onCreate(savedInstanceState); 203 204 NonConfigurationInstances nc = (NonConfigurationInstances) 205 getLastNonConfigurationInstance(); 206 if (nc != null) { 207 mAllLoaderManagers = nc.loaders; 208 } 209 if (savedInstanceState != null) { 210 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG); 211 mFragments.restoreAllState(p, nc != null ? nc.fragments : null); 212 } 213 mFragments.dispatchCreate(); 214 } 215 216 /** 217 * Dispatch to Fragment.onCreateOptionsMenu(). 218 */ 219 @Override 220 public boolean onCreatePanelMenu(int featureId, Menu menu) { 221 if (featureId == Window.FEATURE_OPTIONS_PANEL) { 222 boolean show = super.onCreatePanelMenu(featureId, menu); 223 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater()); 224 if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) { 225 return show; 226 } 227 // Prior to Honeycomb, the framework can't invalidate the options 228 // menu, so we must always say we have one in case the app later 229 // invalidates it and needs to have it shown. 230 return true; 231 } 232 return super.onCreatePanelMenu(featureId, menu); 233 } 234 235 /** 236 * Add support for inflating the <fragment> tag. 237 */ 238 @Override 239 public View onCreateView(String name, Context context, AttributeSet attrs) { 240 if (!"fragment".equals(name)) { 241 return super.onCreateView(name, context, attrs); 242 } 243 244 String fname = attrs.getAttributeValue(null, "class"); 245 TypedArray a = context.obtainStyledAttributes(attrs, FragmentTag.Fragment); 246 if (fname == null) { 247 fname = a.getString(FragmentTag.Fragment_name); 248 } 249 int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID); 250 String tag = a.getString(FragmentTag.Fragment_tag); 251 a.recycle(); 252 253 View parent = null; // NOTE: no way to get parent pre-Honeycomb. 254 int containerId = parent != null ? parent.getId() : 0; 255 if (containerId == View.NO_ID && id == View.NO_ID && tag == null) { 256 throw new IllegalArgumentException(attrs.getPositionDescription() 257 + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname); 258 } 259 260 // If we restored from a previous state, we may already have 261 // instantiated this fragment from the state and should use 262 // that instance instead of making a new one. 263 Fragment fragment = id != View.NO_ID ? mFragments.findFragmentById(id) : null; 264 if (fragment == null && tag != null) { 265 fragment = mFragments.findFragmentByTag(tag); 266 } 267 if (fragment == null && containerId != View.NO_ID) { 268 fragment = mFragments.findFragmentById(containerId); 269 } 270 271 if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x" 272 + Integer.toHexString(id) + " fname=" + fname 273 + " existing=" + fragment); 274 if (fragment == null) { 275 fragment = Fragment.instantiate(this, fname); 276 fragment.mFromLayout = true; 277 fragment.mFragmentId = id != 0 ? id : containerId; 278 fragment.mContainerId = containerId; 279 fragment.mTag = tag; 280 fragment.mInLayout = true; 281 fragment.mFragmentManager = mFragments; 282 fragment.onInflate(this, attrs, fragment.mSavedFragmentState); 283 mFragments.addFragment(fragment, true); 284 285 } else if (fragment.mInLayout) { 286 // A fragment already exists and it is not one we restored from 287 // previous state. 288 throw new IllegalArgumentException(attrs.getPositionDescription() 289 + ": Duplicate id 0x" + Integer.toHexString(id) 290 + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId) 291 + " with another fragment for " + fname); 292 } else { 293 // This fragment was retained from a previous instance; get it 294 // going now. 295 fragment.mInLayout = true; 296 // If this fragment is newly instantiated (either right now, or 297 // from last saved state), then give it the attributes to 298 // initialize itself. 299 if (!fragment.mRetaining) { 300 fragment.onInflate(this, attrs, fragment.mSavedFragmentState); 301 } 302 mFragments.moveToState(fragment); 303 } 304 305 if (fragment.mView == null) { 306 throw new IllegalStateException("Fragment " + fname 307 + " did not create a view."); 308 } 309 if (id != 0) { 310 fragment.mView.setId(id); 311 } 312 if (fragment.mView.getTag() == null) { 313 fragment.mView.setTag(tag); 314 } 315 return fragment.mView; 316 } 317 318 /** 319 * Destroy all fragments and loaders. 320 */ 321 @Override 322 protected void onDestroy() { 323 super.onDestroy(); 324 325 doReallyStop(false); 326 327 mFragments.dispatchDestroy(); 328 if (mLoaderManager != null) { 329 mLoaderManager.doDestroy(); 330 } 331 } 332 333 /** 334 * Take care of calling onBackPressed() for pre-Eclair platforms. 335 */ 336 @Override 337 public boolean onKeyDown(int keyCode, KeyEvent event) { 338 if (android.os.Build.VERSION.SDK_INT < 5 /* ECLAIR */ 339 && keyCode == KeyEvent.KEYCODE_BACK 340 && event.getRepeatCount() == 0) { 341 // Take care of calling this method on earlier versions of 342 // the platform where it doesn't exist. 343 onBackPressed(); 344 return true; 345 } 346 347 return super.onKeyDown(keyCode, event); 348 } 349 350 /** 351 * Dispatch onLowMemory() to all fragments. 352 */ 353 @Override 354 public void onLowMemory() { 355 super.onLowMemory(); 356 mFragments.dispatchLowMemory(); 357 } 358 359 /** 360 * Dispatch context and options menu to fragments. 361 */ 362 @Override 363 public boolean onMenuItemSelected(int featureId, MenuItem item) { 364 if (super.onMenuItemSelected(featureId, item)) { 365 return true; 366 } 367 368 switch (featureId) { 369 case Window.FEATURE_OPTIONS_PANEL: 370 return mFragments.dispatchOptionsItemSelected(item); 371 372 case Window.FEATURE_CONTEXT_MENU: 373 return mFragments.dispatchContextItemSelected(item); 374 375 default: 376 return false; 377 } 378 } 379 380 /** 381 * Call onOptionsMenuClosed() on fragments. 382 */ 383 @Override 384 public void onPanelClosed(int featureId, Menu menu) { 385 switch (featureId) { 386 case Window.FEATURE_OPTIONS_PANEL: 387 mFragments.dispatchOptionsMenuClosed(menu); 388 break; 389 } 390 super.onPanelClosed(featureId, menu); 391 } 392 393 /** 394 * Dispatch onPause() to fragments. 395 */ 396 @Override 397 protected void onPause() { 398 super.onPause(); 399 mResumed = false; 400 if (mHandler.hasMessages(MSG_RESUME_PENDING)) { 401 mHandler.removeMessages(MSG_RESUME_PENDING); 402 onResumeFragments(); 403 } 404 mFragments.dispatchPause(); 405 } 406 407 /** 408 * Handle onNewIntent() to inform the fragment manager that the 409 * state is not saved. If you are handling new intents and may be 410 * making changes to the fragment state, you want to be sure to call 411 * through to the super-class here first. Otherwise, if your state 412 * is saved but the activity is not stopped, you could get an 413 * onNewIntent() call which happens before onResume() and trying to 414 * perform fragment operations at that point will throw IllegalStateException 415 * because the fragment manager thinks the state is still saved. 416 */ 417 @Override 418 protected void onNewIntent(Intent intent) { 419 super.onNewIntent(intent); 420 mFragments.noteStateNotSaved(); 421 } 422 423 /** 424 * Dispatch onResume() to fragments. Note that for better inter-operation 425 * with older versions of the platform, at the point of this call the 426 * fragments attached to the activity are <em>not</em> resumed. This means 427 * that in some cases the previous state may still be saved, not allowing 428 * fragment transactions that modify the state. To correctly interact 429 * with fragments in their proper state, you should instead override 430 * {@link #onResumeFragments()}. 431 */ 432 @Override 433 protected void onResume() { 434 super.onResume(); 435 mHandler.sendEmptyMessage(MSG_RESUME_PENDING); 436 mResumed = true; 437 mFragments.execPendingActions(); 438 } 439 440 /** 441 * Dispatch onResume() to fragments. 442 */ 443 @Override 444 protected void onPostResume() { 445 super.onPostResume(); 446 mHandler.removeMessages(MSG_RESUME_PENDING); 447 onResumeFragments(); 448 mFragments.execPendingActions(); 449 } 450 451 /** 452 * This is the fragment-orientated version of {@link #onResume()} that you 453 * can override to perform operations in the Activity at the same point 454 * where its fragments are resumed. Be sure to always call through to 455 * the super-class. 456 */ 457 protected void onResumeFragments() { 458 mFragments.dispatchResume(); 459 } 460 461 /** 462 * Dispatch onPrepareOptionsMenu() to fragments. 463 */ 464 @Override 465 public boolean onPreparePanel(int featureId, View view, Menu menu) { 466 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { 467 if (mOptionsMenuInvalidated) { 468 mOptionsMenuInvalidated = false; 469 menu.clear(); 470 onCreatePanelMenu(featureId, menu); 471 } 472 boolean goforit = super.onPreparePanel(featureId, view, menu); 473 goforit |= mFragments.dispatchPrepareOptionsMenu(menu); 474 return goforit && menu.hasVisibleItems(); 475 } 476 return super.onPreparePanel(featureId, view, menu); 477 } 478 479 /** 480 * Retain all appropriate fragment and loader state. You can NOT 481 * override this yourself! Use {@link #onRetainCustomNonConfigurationInstance()} 482 * if you want to retain your own state. 483 */ 484 @Override 485 public final Object onRetainNonConfigurationInstance() { 486 if (mStopped) { 487 doReallyStop(true); 488 } 489 490 Object custom = onRetainCustomNonConfigurationInstance(); 491 492 ArrayList<Fragment> fragments = mFragments.retainNonConfig(); 493 boolean retainLoaders = false; 494 if (mAllLoaderManagers != null) { 495 // prune out any loader managers that were already stopped and so 496 // have nothing useful to retain. 497 LoaderManagerImpl loaders[] = new LoaderManagerImpl[mAllLoaderManagers.size()]; 498 mAllLoaderManagers.values().toArray(loaders); 499 if (loaders != null) { 500 for (int i=0; i<loaders.length; i++) { 501 LoaderManagerImpl lm = loaders[i]; 502 if (lm.mRetaining) { 503 retainLoaders = true; 504 } else { 505 lm.doDestroy(); 506 mAllLoaderManagers.remove(lm.mWho); 507 } 508 } 509 } 510 } 511 if (fragments == null && !retainLoaders && custom == null) { 512 return null; 513 } 514 515 NonConfigurationInstances nci = new NonConfigurationInstances(); 516 nci.activity = null; 517 nci.custom = custom; 518 nci.children = null; 519 nci.fragments = fragments; 520 nci.loaders = mAllLoaderManagers; 521 return nci; 522 } 523 524 /** 525 * Save all appropriate fragment state. 526 */ 527 @Override 528 protected void onSaveInstanceState(Bundle outState) { 529 super.onSaveInstanceState(outState); 530 Parcelable p = mFragments.saveAllState(); 531 if (p != null) { 532 outState.putParcelable(FRAGMENTS_TAG, p); 533 } 534 } 535 536 /** 537 * Dispatch onStart() to all fragments. Ensure any created loaders are 538 * now started. 539 */ 540 @Override 541 protected void onStart() { 542 super.onStart(); 543 544 mStopped = false; 545 mReallyStopped = false; 546 mHandler.removeMessages(MSG_REALLY_STOPPED); 547 548 if (!mCreated) { 549 mCreated = true; 550 mFragments.dispatchActivityCreated(); 551 } 552 553 mFragments.noteStateNotSaved(); 554 mFragments.execPendingActions(); 555 556 if (!mLoadersStarted) { 557 mLoadersStarted = true; 558 if (mLoaderManager != null) { 559 mLoaderManager.doStart(); 560 } else if (!mCheckedForLoaderManager) { 561 mLoaderManager = getLoaderManager(null, mLoadersStarted, false); 562 // the returned loader manager may be a new one, so we have to start it 563 if ((mLoaderManager != null) && (!mLoaderManager.mStarted)) { 564 mLoaderManager.doStart(); 565 } 566 } 567 mCheckedForLoaderManager = true; 568 } 569 // NOTE: HC onStart goes here. 570 571 mFragments.dispatchStart(); 572 if (mAllLoaderManagers != null) { 573 LoaderManagerImpl loaders[] = new LoaderManagerImpl[mAllLoaderManagers.size()]; 574 mAllLoaderManagers.values().toArray(loaders); 575 if (loaders != null) { 576 for (int i=0; i<loaders.length; i++) { 577 LoaderManagerImpl lm = loaders[i]; 578 lm.finishRetain(); 579 lm.doReportStart(); 580 } 581 } 582 } 583 } 584 585 /** 586 * Dispatch onStop() to all fragments. Ensure all loaders are stopped. 587 */ 588 @Override 589 protected void onStop() { 590 super.onStop(); 591 592 mStopped = true; 593 mHandler.sendEmptyMessage(MSG_REALLY_STOPPED); 594 595 mFragments.dispatchStop(); 596 } 597 598 // ------------------------------------------------------------------------ 599 // NEW METHODS 600 // ------------------------------------------------------------------------ 601 602 /** 603 * Use this instead of {@link #onRetainNonConfigurationInstance()}. 604 * Retrieve later with {@link #getLastCustomNonConfigurationInstance()}. 605 */ 606 public Object onRetainCustomNonConfigurationInstance() { 607 return null; 608 } 609 610 /** 611 * Return the value previously returned from 612 * {@link #onRetainCustomNonConfigurationInstance()}. 613 */ 614 public Object getLastCustomNonConfigurationInstance() { 615 NonConfigurationInstances nc = (NonConfigurationInstances) 616 getLastNonConfigurationInstance(); 617 return nc != null ? nc.custom : null; 618 } 619 620 /** 621 * Invalidate the activity's options menu. This will cause relevant presentations 622 * of the menu to fully update via calls to onCreateOptionsMenu and 623 * onPrepareOptionsMenu the next time the menu is requested. 624 */ 625 public void supportInvalidateOptionsMenu() { 626 if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) { 627 // If we are running on HC or greater, we can use the framework 628 // API to invalidate the options menu. 629 ActivityCompatHoneycomb.invalidateOptionsMenu(this); 630 return; 631 } 632 633 // Whoops, older platform... we'll use a hack, to manually rebuild 634 // the options menu the next time it is prepared. 635 mOptionsMenuInvalidated = true; 636 } 637 638 /** 639 * Support library version of onPrepareOptionsMenu. 640 * 641 * Prepare the Screen's standard options menu to be displayed. This is 642 * called right before the menu is shown, every time it is shown. You can 643 * use this method to efficiently enable/disable items or otherwise 644 * dynamically modify the contents. 645 * 646 * <p>The default implementation updates the system menu items based on the 647 * activity's state. Deriving classes should always call through to the 648 * base class implementation. 649 * 650 * @param menu The options menu as last shown or first initialized by 651 * onCreateSupportOptionsMenu(). 652 * 653 * @return You must return true for the menu to be displayed; 654 * if you return false it will not be shown. 655 * 656 * @see #onPrepareSupportOptionsMenu 657 * @see #onCreateSupportOptionsMenu 658 */ 659 public boolean onPrepareSupportOptionsMenu(android.support.v4.view.Menu menu) { 660 return true; 661 } 662 663 /** 664 * Support library version of onCreateOptionsMenu. 665 * 666 * Initialize the contents of the Activity's standard options menu. You 667 * should place your menu items in to <var>menu</var>. 668 * 669 * <p>This is only called once, the first time the options menu is 670 * displayed. To update the menu every time it is displayed, see 671 * {@link #onPrepareSupportOptionsMenu}. 672 * 673 * <p>The default implementation populates the menu with standard system 674 * menu items. These are placed in the {@link android.support.v4.view.Menu#CATEGORY_SYSTEM} 675 * group so that they will be correctly ordered with application-defined menu items. 676 * Deriving classes should always call through to the base implementation. 677 * 678 * <p>You can safely hold on to <var>menu</var> (and any items created 679 * from it), making modifications to it as desired, until the next 680 * time onCreateSupportOptionsMenu() is called. 681 * 682 * <p>When you add items to the menu, you can implement the Activity's 683 * {@link #onSupportOptionsItemSelected} method to handle them there. 684 * 685 * @param menu The options menu in which you place your items. 686 * 687 * @return You must return true for the menu to be displayed; 688 * if you return false it will not be shown. 689 * 690 * @see #onCreateSupportOptionsMenu 691 * @see #onPrepareSupportOptionsMenu 692 * @see #onSupportOptionsItemSelected 693 */ 694 public boolean onCreateSupportOptionsMenu(android.support.v4.view.Menu menu) { 695 return true; 696 } 697 698 /** 699 * This hook is called whenever an item in your options menu is selected. 700 * The default implementation simply returns false to have the normal 701 * processing happen (calling the item's Runnable or sending a message to 702 * its Handler as appropriate). You can use this method for any items 703 * for which you would like to do processing without those other 704 * facilities. 705 * 706 * <p>Derived classes should call through to the base class for it to 707 * perform the default menu handling.</p> 708 * 709 * @param item The menu item that was selected. 710 * 711 * @return boolean Return false to allow normal menu processing to 712 * proceed, true to consume it here. 713 * 714 * @see #onCreateSupportOptionsMenu 715 */ 716 public boolean onSupportOptionsItemSelected(android.support.v4.view.MenuItem item) { 717 return false; 718 } 719 720 /** 721 * Returns a {@link MenuInflater} with this context. 722 */ 723 public MenuInflater getSupportMenuInflater() { 724 if (mMenuInflater == null) { 725 mMenuInflater = createSupportMenuInflater(); 726 } 727 return mMenuInflater; 728 } 729 730 MenuInflater createSupportMenuInflater() { 731 return null; 732 } 733 734 /** 735 * Print the Activity's state into the given stream. This gets invoked if 736 * you run "adb shell dumpsys activity <activity_component_name>". 737 * 738 * @param prefix Desired prefix to prepend at each line of output. 739 * @param fd The raw file descriptor that the dump is being sent to. 740 * @param writer The PrintWriter to which you should dump your state. This will be 741 * closed for you after you return. 742 * @param args additional arguments to the dump request. 743 */ 744 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 745 if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) { 746 // XXX This can only work if we can call the super-class impl. :/ 747 //ActivityCompatHoneycomb.dump(this, prefix, fd, writer, args); 748 } 749 writer.print(prefix); writer.print("Local FragmentActivity "); 750 writer.print(Integer.toHexString(System.identityHashCode(this))); 751 writer.println(" State:"); 752 String innerPrefix = prefix + " "; 753 writer.print(innerPrefix); writer.print("mCreated="); 754 writer.print(mCreated); writer.print("mResumed="); 755 writer.print(mResumed); writer.print(" mStopped="); 756 writer.print(mStopped); writer.print(" mReallyStopped="); 757 writer.println(mReallyStopped); 758 writer.print(innerPrefix); writer.print("mLoadersStarted="); 759 writer.println(mLoadersStarted); 760 if (mLoaderManager != null) { 761 writer.print(prefix); writer.print("Loader Manager "); 762 writer.print(Integer.toHexString(System.identityHashCode(mLoaderManager))); 763 writer.println(":"); 764 mLoaderManager.dump(prefix + " ", fd, writer, args); 765 } 766 mFragments.dump(prefix, fd, writer, args); 767 writer.print(prefix); writer.println("View Hierarchy:"); 768 dumpViewHierarchy(prefix + " ", writer, getWindow().getDecorView()); 769 } 770 771 private static String viewToString(View view) { 772 StringBuilder out = new StringBuilder(128); 773 out.append(view.getClass().getName()); 774 out.append('{'); 775 out.append(Integer.toHexString(System.identityHashCode(view))); 776 out.append(' '); 777 switch (view.getVisibility()) { 778 case View.VISIBLE: out.append('V'); break; 779 case View.INVISIBLE: out.append('I'); break; 780 case View.GONE: out.append('G'); break; 781 default: out.append('.'); break; 782 } 783 out.append(view.isFocusable() ? 'F' : '.'); 784 out.append(view.isEnabled() ? 'E' : '.'); 785 out.append(view.willNotDraw() ? '.' : 'D'); 786 out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.'); 787 out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.'); 788 out.append(view.isClickable() ? 'C' : '.'); 789 out.append(view.isLongClickable() ? 'L' : '.'); 790 out.append(' '); 791 out.append(view.isFocused() ? 'F' : '.'); 792 out.append(view.isSelected() ? 'S' : '.'); 793 out.append(view.isPressed() ? 'P' : '.'); 794 out.append(' '); 795 out.append(view.getLeft()); 796 out.append(','); 797 out.append(view.getTop()); 798 out.append('-'); 799 out.append(view.getRight()); 800 out.append(','); 801 out.append(view.getBottom()); 802 final int id = view.getId(); 803 if (id != View.NO_ID) { 804 out.append(" #"); 805 out.append(Integer.toHexString(id)); 806 final Resources r = view.getResources(); 807 if (id != 0 && r != null) { 808 try { 809 String pkgname; 810 switch (id&0xff000000) { 811 case 0x7f000000: 812 pkgname="app"; 813 break; 814 case 0x01000000: 815 pkgname="android"; 816 break; 817 default: 818 pkgname = r.getResourcePackageName(id); 819 break; 820 } 821 String typename = r.getResourceTypeName(id); 822 String entryname = r.getResourceEntryName(id); 823 out.append(" "); 824 out.append(pkgname); 825 out.append(":"); 826 out.append(typename); 827 out.append("/"); 828 out.append(entryname); 829 } catch (Resources.NotFoundException e) { 830 } 831 } 832 } 833 out.append("}"); 834 return out.toString(); 835 } 836 837 private void dumpViewHierarchy(String prefix, PrintWriter writer, View view) { 838 writer.print(prefix); 839 if (view == null) { 840 writer.println("null"); 841 return; 842 } 843 writer.println(viewToString(view)); 844 if (!(view instanceof ViewGroup)) { 845 return; 846 } 847 ViewGroup grp = (ViewGroup)view; 848 final int N = grp.getChildCount(); 849 if (N <= 0) { 850 return; 851 } 852 prefix = prefix + " "; 853 for (int i=0; i<N; i++) { 854 dumpViewHierarchy(prefix, writer, grp.getChildAt(i)); 855 } 856 } 857 858 void doReallyStop(boolean retaining) { 859 if (!mReallyStopped) { 860 mReallyStopped = true; 861 mRetaining = retaining; 862 mHandler.removeMessages(MSG_REALLY_STOPPED); 863 onReallyStop(); 864 } 865 } 866 867 /** 868 * Pre-HC, we didn't have a way to determine whether an activity was 869 * being stopped for a config change or not until we saw 870 * onRetainNonConfigurationInstance() called after onStop(). However 871 * we need to know this, to know whether to retain fragments. This will 872 * tell us what we need to know. 873 */ 874 void onReallyStop() { 875 if (mLoadersStarted) { 876 mLoadersStarted = false; 877 if (mLoaderManager != null) { 878 if (!mRetaining) { 879 mLoaderManager.doStop(); 880 } else { 881 mLoaderManager.doRetain(); 882 } 883 } 884 } 885 886 mFragments.dispatchReallyStop(); 887 } 888 889 // ------------------------------------------------------------------------ 890 // FRAGMENT SUPPORT 891 // ------------------------------------------------------------------------ 892 893 /** 894 * Called when a fragment is attached to the activity. 895 */ 896 public void onAttachFragment(Fragment fragment) { 897 } 898 899 /** 900 * Return the FragmentManager for interacting with fragments associated 901 * with this activity. 902 */ 903 public FragmentManager getSupportFragmentManager() { 904 return mFragments; 905 } 906 907 /** 908 * Modifies the standard behavior to allow results to be delivered to fragments. 909 * This imposes a restriction that requestCode be <= 0xffff. 910 */ 911 @Override 912 public void startActivityForResult(Intent intent, int requestCode) { 913 if (requestCode != -1 && (requestCode&0xffff0000) != 0) { 914 throw new IllegalArgumentException("Can only use lower 16 bits for requestCode"); 915 } 916 super.startActivityForResult(intent, requestCode); 917 } 918 919 /** 920 * Called by Fragment.startActivityForResult() to implement its behavior. 921 */ 922 public void startActivityFromFragment(Fragment fragment, Intent intent, 923 int requestCode) { 924 if (requestCode == -1) { 925 super.startActivityForResult(intent, -1); 926 return; 927 } 928 if ((requestCode&0xffff0000) != 0) { 929 throw new IllegalArgumentException("Can only use lower 16 bits for requestCode"); 930 } 931 super.startActivityForResult(intent, ((fragment.mIndex+1)<<16) + (requestCode&0xffff)); 932 } 933 934 void invalidateSupportFragment(String who) { 935 //Log.v(TAG, "invalidateSupportFragment: who=" + who); 936 if (mAllLoaderManagers != null) { 937 LoaderManagerImpl lm = mAllLoaderManagers.get(who); 938 if (lm != null && !lm.mRetaining) { 939 lm.doDestroy(); 940 mAllLoaderManagers.remove(who); 941 } 942 } 943 } 944 945 // ------------------------------------------------------------------------ 946 // LOADER SUPPORT 947 // ------------------------------------------------------------------------ 948 949 /** 950 * Return the LoaderManager for this fragment, creating it if needed. 951 */ 952 public LoaderManager getSupportLoaderManager() { 953 if (mLoaderManager != null) { 954 return mLoaderManager; 955 } 956 mCheckedForLoaderManager = true; 957 mLoaderManager = getLoaderManager(null, mLoadersStarted, true); 958 return mLoaderManager; 959 } 960 961 LoaderManagerImpl getLoaderManager(String who, boolean started, boolean create) { 962 if (mAllLoaderManagers == null) { 963 mAllLoaderManagers = new HashMap<String, LoaderManagerImpl>(); 964 } 965 LoaderManagerImpl lm = mAllLoaderManagers.get(who); 966 if (lm == null) { 967 if (create) { 968 lm = new LoaderManagerImpl(who, this, started); 969 mAllLoaderManagers.put(who, lm); 970 } 971 } else { 972 lm.updateActivity(this); 973 } 974 return lm; 975 } 976} 977