/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.support.v4.app; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Parcelable; import android.support.annotation.NonNull; import android.support.v4.util.SimpleArrayMap; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.ArrayList; /** * Base class for activities that want to use the support-based * {@link android.support.v4.app.Fragment} and * {@link android.support.v4.content.Loader} APIs. * *
When using this class as opposed to new platform's built-in fragment * and loader support, you must use the {@link #getSupportFragmentManager()} * and {@link #getSupportLoaderManager()} methods respectively to access * those features. * *
Note: If you want to implement an activity that includes * an action bar, you should instead use * the {@link android.support.v7.app.ActionBarActivity} class, which is a subclass of this one, * so allows you to use {@link android.support.v4.app.Fragment} APIs on API level 7 and higher.
* *Known limitations:
*When using the <fragment>
tag, this implementation can not
* use the parent view's ID as the new fragment's ID. You must explicitly
* specify an ID (or tag) in the <fragment>
.
Prior to Honeycomb (3.0), an activity's state was saved before pausing. * Fragments are a significant amount of new state, and dynamic enough that one * often wants them to change between pausing and stopping. These classes * throw an exception if you try to change the fragment state after it has been * saved, to avoid accidental loss of UI state. However this is too restrictive * prior to Honeycomb, where the state is saved before pausing. To address this, * when running on platforms prior to Honeycomb an exception will not be thrown * if you change fragments between the state save and the activity being stopped. * This means that in some cases if the activity is restored from its last saved * state, this may be a snapshot slightly before what the user last saw.
*On Android 4.4 or lower, this method only finishes the Activity with no * special exit transition.
*/ public void supportFinishAfterTransition() { ActivityCompat.finishAfterTransition(this); } /** * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, * android.view.View, String)} was used to start an Activity, callback * will be called to handle shared elements on the launched Activity. This requires * {@link Window#FEATURE_CONTENT_TRANSITIONS}. * * @param callback Used to manipulate shared element transitions on the launched Activity. */ public void setEnterSharedElementCallback(SharedElementCallback callback) { ActivityCompat.setEnterSharedElementCallback(this, callback); } /** * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, * android.view.View, String)} was used to start an Activity, listener * will be called to handle shared elements on the launching Activity. Most * calls will only come when returning from the started Activity. * This requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. * * @param listener Used to manipulate shared element transitions on the launching Activity. */ public void setExitSharedElementCallback(SharedElementCallback listener) { ActivityCompat.setExitSharedElementCallback(this, listener); } /** * Support library version of {@link android.app.Activity#postponeEnterTransition()} that works * only on API 21 and later. */ public void supportPostponeEnterTransition() { ActivityCompat.postponeEnterTransition(this); } /** * Support library version of {@link android.app.Activity#startPostponedEnterTransition()} * that only works with API 21 and later. */ public void supportStartPostponedEnterTransition() { ActivityCompat.startPostponedEnterTransition(this); } /** * Dispatch configuration change to all fragments. */ @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mFragments.dispatchConfigurationChanged(newConfig); } /** * Perform initialization of all fragments and loaders. */ @Override protected void onCreate(Bundle savedInstanceState) { mFragments.attachActivity(this, mContainer, null); // Old versions of the platform didn't do this! if (getLayoutInflater().getFactory() == null) { getLayoutInflater().setFactory(this); } super.onCreate(savedInstanceState); NonConfigurationInstances nc = (NonConfigurationInstances) getLastNonConfigurationInstance(); if (nc != null) { mAllLoaderManagers = nc.loaders; } if (savedInstanceState != null) { Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG); mFragments.restoreAllState(p, nc != null ? nc.fragments : null); } mFragments.dispatchCreate(); } /** * Dispatch to Fragment.onCreateOptionsMenu(). */ @Override public boolean onCreatePanelMenu(int featureId, Menu menu) { if (featureId == Window.FEATURE_OPTIONS_PANEL) { boolean show = super.onCreatePanelMenu(featureId, menu); show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater()); if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) { return show; } // Prior to Honeycomb, the framework can't invalidate the options // menu, so we must always say we have one in case the app later // invalidates it and needs to have it shown. return true; } return super.onCreatePanelMenu(featureId, menu); } /** * Add support for inflating the <fragment> tag. */ @Override public View onCreateView(String name, @NonNull Context context, @NonNull AttributeSet attrs) { if (!"fragment".equals(name)) { return super.onCreateView(name, context, attrs); } final View v = mFragments.onCreateView(name, context, attrs); if (v == null) { return super.onCreateView(name, context, attrs); } return v; } /** * Destroy all fragments and loaders. */ @Override protected void onDestroy() { super.onDestroy(); doReallyStop(false); mFragments.dispatchDestroy(); if (mLoaderManager != null) { mLoaderManager.doDestroy(); } } /** * Take care of calling onBackPressed() for pre-Eclair platforms. */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (android.os.Build.VERSION.SDK_INT < 5 /* ECLAIR */ && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { // Take care of calling this method on earlier versions of // the platform where it doesn't exist. onBackPressed(); return true; } return super.onKeyDown(keyCode, event); } /** * Dispatch onLowMemory() to all fragments. */ @Override public void onLowMemory() { super.onLowMemory(); mFragments.dispatchLowMemory(); } /** * Dispatch context and options menu to fragments. */ @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if (super.onMenuItemSelected(featureId, item)) { return true; } switch (featureId) { case Window.FEATURE_OPTIONS_PANEL: return mFragments.dispatchOptionsItemSelected(item); case Window.FEATURE_CONTEXT_MENU: return mFragments.dispatchContextItemSelected(item); default: return false; } } /** * Call onOptionsMenuClosed() on fragments. */ @Override public void onPanelClosed(int featureId, Menu menu) { switch (featureId) { case Window.FEATURE_OPTIONS_PANEL: mFragments.dispatchOptionsMenuClosed(menu); break; } super.onPanelClosed(featureId, menu); } /** * Dispatch onPause() to fragments. */ @Override protected void onPause() { super.onPause(); mResumed = false; if (mHandler.hasMessages(MSG_RESUME_PENDING)) { mHandler.removeMessages(MSG_RESUME_PENDING); onResumeFragments(); } mFragments.dispatchPause(); } /** * Handle onNewIntent() to inform the fragment manager that the * state is not saved. If you are handling new intents and may be * making changes to the fragment state, you want to be sure to call * through to the super-class here first. Otherwise, if your state * is saved but the activity is not stopped, you could get an * onNewIntent() call which happens before onResume() and trying to * perform fragment operations at that point will throw IllegalStateException * because the fragment manager thinks the state is still saved. */ @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); mFragments.noteStateNotSaved(); } /** * Dispatch onResume() to fragments. Note that for better inter-operation * with older versions of the platform, at the point of this call the * fragments attached to the activity are not resumed. This means * that in some cases the previous state may still be saved, not allowing * fragment transactions that modify the state. To correctly interact * with fragments in their proper state, you should instead override * {@link #onResumeFragments()}. */ @Override protected void onResume() { super.onResume(); mHandler.sendEmptyMessage(MSG_RESUME_PENDING); mResumed = true; mFragments.execPendingActions(); } /** * Dispatch onResume() to fragments. */ @Override protected void onPostResume() { super.onPostResume(); mHandler.removeMessages(MSG_RESUME_PENDING); onResumeFragments(); mFragments.execPendingActions(); } /** * This is the fragment-orientated version of {@link #onResume()} that you * can override to perform operations in the Activity at the same point * where its fragments are resumed. Be sure to always call through to * the super-class. */ protected void onResumeFragments() { mFragments.dispatchResume(); } /** * Dispatch onPrepareOptionsMenu() to fragments. */ @Override public boolean onPreparePanel(int featureId, View view, Menu menu) { if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { if (mOptionsMenuInvalidated) { mOptionsMenuInvalidated = false; menu.clear(); onCreatePanelMenu(featureId, menu); } boolean goforit = onPrepareOptionsPanel(view, menu); goforit |= mFragments.dispatchPrepareOptionsMenu(menu); return goforit; } return super.onPreparePanel(featureId, view, menu); } /** * @hide */ protected boolean onPrepareOptionsPanel(View view, Menu menu) { return super.onPreparePanel(Window.FEATURE_OPTIONS_PANEL, view, menu); } /** * Retain all appropriate fragment and loader state. You can NOT * override this yourself! Use {@link #onRetainCustomNonConfigurationInstance()} * if you want to retain your own state. */ @Override public final Object onRetainNonConfigurationInstance() { if (mStopped) { doReallyStop(true); } Object custom = onRetainCustomNonConfigurationInstance(); ArrayList