1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.app; 18 19 import android.annotation.Nullable; 20 import android.compat.annotation.UnsupportedAppUsage; 21 import android.content.Context; 22 import android.content.res.Configuration; 23 import android.os.Build; 24 import android.os.Bundle; 25 import android.os.Parcelable; 26 import android.util.ArrayMap; 27 import android.util.AttributeSet; 28 import android.view.Menu; 29 import android.view.MenuInflater; 30 import android.view.MenuItem; 31 import android.view.View; 32 33 import java.io.FileDescriptor; 34 import java.io.PrintWriter; 35 import java.util.List; 36 37 /** 38 * Provides integration points with a {@link FragmentManager} for a fragment host. 39 * <p> 40 * It is the responsibility of the host to take care of the Fragment's lifecycle. 41 * The methods provided by {@link FragmentController} are for that purpose. 42 * 43 * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">Support Library</a> 44 * {@link android.support.v4.app.FragmentController} 45 */ 46 @Deprecated 47 public class FragmentController { 48 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 49 private final FragmentHostCallback<?> mHost; 50 51 /** 52 * Returns a {@link FragmentController}. 53 */ createController(FragmentHostCallback<?> callbacks)54 public static final FragmentController createController(FragmentHostCallback<?> callbacks) { 55 return new FragmentController(callbacks); 56 } 57 FragmentController(FragmentHostCallback<?> callbacks)58 private FragmentController(FragmentHostCallback<?> callbacks) { 59 mHost = callbacks; 60 } 61 62 /** 63 * Returns a {@link FragmentManager} for this controller. 64 */ getFragmentManager()65 public FragmentManager getFragmentManager() { 66 return mHost.getFragmentManagerImpl(); 67 } 68 69 /** 70 * Returns a {@link LoaderManager}. 71 */ getLoaderManager()72 public LoaderManager getLoaderManager() { 73 return mHost.getLoaderManagerImpl(); 74 } 75 76 /** 77 * Returns a fragment with the given identifier. 78 */ 79 @Nullable findFragmentByWho(String who)80 public Fragment findFragmentByWho(String who) { 81 return mHost.mFragmentManager.findFragmentByWho(who); 82 } 83 84 /** 85 * Attaches the host to the FragmentManager for this controller. The host must be 86 * attached before the FragmentManager can be used to manage Fragments. 87 * */ attachHost(Fragment parent)88 public void attachHost(Fragment parent) { 89 mHost.mFragmentManager.attachController( 90 mHost, mHost /*container*/, parent); 91 } 92 93 /** 94 * Instantiates a Fragment's view. 95 * 96 * @param parent The parent that the created view will be placed 97 * in; <em>note that this may be null</em>. 98 * @param name Tag name to be inflated. 99 * @param context The context the view is being created in. 100 * @param attrs Inflation attributes as specified in XML file. 101 * 102 * @return view the newly created view 103 */ onCreateView(View parent, String name, Context context, AttributeSet attrs)104 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 105 return mHost.mFragmentManager.onCreateView(parent, name, context, attrs); 106 } 107 108 /** 109 * Marks the fragment state as unsaved. This allows for "state loss" detection. 110 */ noteStateNotSaved()111 public void noteStateNotSaved() { 112 mHost.mFragmentManager.noteStateNotSaved(); 113 } 114 115 /** 116 * Saves the state for all Fragments. 117 */ saveAllState()118 public Parcelable saveAllState() { 119 return mHost.mFragmentManager.saveAllState(); 120 } 121 122 /** 123 * Restores the saved state for all Fragments. The given Fragment list are Fragment 124 * instances retained across configuration changes. 125 * 126 * @see #retainNonConfig() 127 * 128 * @deprecated use {@link #restoreAllState(Parcelable, FragmentManagerNonConfig)} 129 */ 130 @Deprecated restoreAllState(Parcelable state, List<Fragment> nonConfigList)131 public void restoreAllState(Parcelable state, List<Fragment> nonConfigList) { 132 mHost.mFragmentManager.restoreAllState(state, 133 new FragmentManagerNonConfig(nonConfigList, null)); 134 } 135 136 /** 137 * Restores the saved state for all Fragments. The given FragmentManagerNonConfig are Fragment 138 * instances retained across configuration changes, including nested fragments 139 * 140 * @see #retainNestedNonConfig() 141 */ restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig)142 public void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) { 143 mHost.mFragmentManager.restoreAllState(state, nonConfig); 144 } 145 146 /** 147 * Returns a list of Fragments that have opted to retain their instance across 148 * configuration changes. 149 * 150 * @deprecated use {@link #retainNestedNonConfig()} to also track retained 151 * nested child fragments 152 */ 153 @Deprecated retainNonConfig()154 public List<Fragment> retainNonConfig() { 155 return mHost.mFragmentManager.retainNonConfig().getFragments(); 156 } 157 158 /** 159 * Returns a nested tree of Fragments that have opted to retain their instance across 160 * configuration changes. 161 */ retainNestedNonConfig()162 public FragmentManagerNonConfig retainNestedNonConfig() { 163 return mHost.mFragmentManager.retainNonConfig(); 164 } 165 166 /** 167 * Moves all Fragments managed by the controller's FragmentManager 168 * into the create state. 169 * <p>Call when Fragments should be created. 170 * 171 * @see Fragment#onCreate(Bundle) 172 */ dispatchCreate()173 public void dispatchCreate() { 174 mHost.mFragmentManager.dispatchCreate(); 175 } 176 177 /** 178 * Moves all Fragments managed by the controller's FragmentManager 179 * into the activity created state. 180 * <p>Call when Fragments should be informed their host has been created. 181 * 182 * @see Fragment#onActivityCreated(Bundle) 183 */ dispatchActivityCreated()184 public void dispatchActivityCreated() { 185 mHost.mFragmentManager.dispatchActivityCreated(); 186 } 187 188 /** 189 * Moves all Fragments managed by the controller's FragmentManager 190 * into the start state. 191 * <p>Call when Fragments should be started. 192 * 193 * @see Fragment#onStart() 194 */ dispatchStart()195 public void dispatchStart() { 196 mHost.mFragmentManager.dispatchStart(); 197 } 198 199 /** 200 * Moves all Fragments managed by the controller's FragmentManager 201 * into the resume state. 202 * <p>Call when Fragments should be resumed. 203 * 204 * @see Fragment#onResume() 205 */ dispatchResume()206 public void dispatchResume() { 207 mHost.mFragmentManager.dispatchResume(); 208 } 209 210 /** 211 * Moves all Fragments managed by the controller's FragmentManager 212 * into the pause state. 213 * <p>Call when Fragments should be paused. 214 * 215 * @see Fragment#onPause() 216 */ dispatchPause()217 public void dispatchPause() { 218 mHost.mFragmentManager.dispatchPause(); 219 } 220 221 /** 222 * Moves all Fragments managed by the controller's FragmentManager 223 * into the stop state. 224 * <p>Call when Fragments should be stopped. 225 * 226 * @see Fragment#onStop() 227 */ dispatchStop()228 public void dispatchStop() { 229 mHost.mFragmentManager.dispatchStop(); 230 } 231 232 /** 233 * Moves all Fragments managed by the controller's FragmentManager 234 * into the destroy view state. 235 * <p>Call when the Fragment's views should be destroyed. 236 * 237 * @see Fragment#onDestroyView() 238 */ dispatchDestroyView()239 public void dispatchDestroyView() { 240 mHost.mFragmentManager.dispatchDestroyView(); 241 } 242 243 /** 244 * Moves all Fragments managed by the controller's FragmentManager 245 * into the destroy state. 246 * <p>Call when Fragments should be destroyed. 247 * 248 * @see Fragment#onDestroy() 249 */ dispatchDestroy()250 public void dispatchDestroy() { 251 mHost.mFragmentManager.dispatchDestroy(); 252 } 253 254 /** 255 * Lets all Fragments managed by the controller's FragmentManager know the multi-window mode of 256 * the activity changed. 257 * <p>Call when the multi-window mode of the activity changed. 258 * 259 * @see Fragment#onMultiWindowModeChanged 260 * @deprecated use {@link #dispatchMultiWindowModeChanged(boolean, Configuration)} 261 */ 262 @Deprecated dispatchMultiWindowModeChanged(boolean isInMultiWindowMode)263 public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) { 264 mHost.mFragmentManager.dispatchMultiWindowModeChanged(isInMultiWindowMode); 265 } 266 267 /** 268 * Lets all Fragments managed by the controller's FragmentManager know the multi-window mode of 269 * the activity changed. 270 * <p>Call when the multi-window mode of the activity changed. 271 * 272 * @see Fragment#onMultiWindowModeChanged 273 */ dispatchMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig)274 public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode, 275 Configuration newConfig) { 276 mHost.mFragmentManager.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig); 277 } 278 279 /** 280 * Lets all Fragments managed by the controller's FragmentManager know the picture-in-picture 281 * mode of the activity changed. 282 * <p>Call when the picture-in-picture mode of the activity changed. 283 * 284 * @see Fragment#onPictureInPictureModeChanged 285 * @deprecated use {@link #dispatchPictureInPictureModeChanged(boolean, Configuration)} 286 */ 287 @Deprecated dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode)288 public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) { 289 mHost.mFragmentManager.dispatchPictureInPictureModeChanged(isInPictureInPictureMode); 290 } 291 292 /** 293 * Lets all Fragments managed by the controller's FragmentManager know the picture-in-picture 294 * mode of the activity changed. 295 * <p>Call when the picture-in-picture mode of the activity changed. 296 * 297 * @see Fragment#onPictureInPictureModeChanged 298 */ dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig)299 public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode, 300 Configuration newConfig) { 301 mHost.mFragmentManager.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, 302 newConfig); 303 } 304 305 /** 306 * Lets all Fragments managed by the controller's FragmentManager 307 * know a configuration change occurred. 308 * <p>Call when there is a configuration change. 309 * 310 * @see Fragment#onConfigurationChanged(Configuration) 311 */ dispatchConfigurationChanged(Configuration newConfig)312 public void dispatchConfigurationChanged(Configuration newConfig) { 313 mHost.mFragmentManager.dispatchConfigurationChanged(newConfig); 314 } 315 316 /** 317 * Lets all Fragments managed by the controller's FragmentManager 318 * know the device is in a low memory condition. 319 * <p>Call when the device is low on memory and Fragment's should trim 320 * their memory usage. 321 * 322 * @see Fragment#onLowMemory() 323 */ dispatchLowMemory()324 public void dispatchLowMemory() { 325 mHost.mFragmentManager.dispatchLowMemory(); 326 } 327 328 /** 329 * Lets all Fragments managed by the controller's FragmentManager 330 * know they should trim their memory usage. 331 * <p>Call when the Fragment can release allocated memory [such as if 332 * the Fragment is in the background]. 333 * 334 * @see Fragment#onTrimMemory(int) 335 */ dispatchTrimMemory(int level)336 public void dispatchTrimMemory(int level) { 337 mHost.mFragmentManager.dispatchTrimMemory(level); 338 } 339 340 /** 341 * Lets all Fragments managed by the controller's FragmentManager 342 * know they should create an options menu. 343 * <p>Call when the Fragment should create an options menu. 344 * 345 * @return {@code true} if the options menu contains items to display 346 * @see Fragment#onCreateOptionsMenu(Menu, MenuInflater) 347 */ dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater)348 public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { 349 return mHost.mFragmentManager.dispatchCreateOptionsMenu(menu, inflater); 350 } 351 352 /** 353 * Lets all Fragments managed by the controller's FragmentManager 354 * know they should prepare their options menu for display. 355 * <p>Call immediately before displaying the Fragment's options menu. 356 * 357 * @return {@code true} if the options menu contains items to display 358 * @see Fragment#onPrepareOptionsMenu(Menu) 359 */ dispatchPrepareOptionsMenu(Menu menu)360 public boolean dispatchPrepareOptionsMenu(Menu menu) { 361 return mHost.mFragmentManager.dispatchPrepareOptionsMenu(menu); 362 } 363 364 /** 365 * Sends an option item selection event to the Fragments managed by the 366 * controller's FragmentManager. Once the event has been consumed, 367 * no additional handling will be performed. 368 * <p>Call immediately after an options menu item has been selected 369 * 370 * @return {@code true} if the options menu selection event was consumed 371 * @see Fragment#onOptionsItemSelected(MenuItem) 372 */ dispatchOptionsItemSelected(MenuItem item)373 public boolean dispatchOptionsItemSelected(MenuItem item) { 374 return mHost.mFragmentManager.dispatchOptionsItemSelected(item); 375 } 376 377 /** 378 * Sends a context item selection event to the Fragments managed by the 379 * controller's FragmentManager. Once the event has been consumed, 380 * no additional handling will be performed. 381 * <p>Call immediately after an options menu item has been selected 382 * 383 * @return {@code true} if the context menu selection event was consumed 384 * @see Fragment#onContextItemSelected(MenuItem) 385 */ dispatchContextItemSelected(MenuItem item)386 public boolean dispatchContextItemSelected(MenuItem item) { 387 return mHost.mFragmentManager.dispatchContextItemSelected(item); 388 } 389 390 /** 391 * Lets all Fragments managed by the controller's FragmentManager 392 * know their options menu has closed. 393 * <p>Call immediately after closing the Fragment's options menu. 394 * 395 * @see Fragment#onOptionsMenuClosed(Menu) 396 */ dispatchOptionsMenuClosed(Menu menu)397 public void dispatchOptionsMenuClosed(Menu menu) { 398 mHost.mFragmentManager.dispatchOptionsMenuClosed(menu); 399 } 400 401 /** 402 * Execute any pending actions for the Fragments managed by the 403 * controller's FragmentManager. 404 * <p>Call when queued actions can be performed [eg when the 405 * Fragment moves into a start or resume state]. 406 * @return {@code true} if queued actions were performed 407 */ execPendingActions()408 public boolean execPendingActions() { 409 return mHost.mFragmentManager.execPendingActions(); 410 } 411 412 /** 413 * Starts the loaders. 414 */ doLoaderStart()415 public void doLoaderStart() { 416 mHost.doLoaderStart(); 417 } 418 419 /** 420 * Stops the loaders, optionally retaining their state. This is useful for keeping the 421 * loader state across configuration changes. 422 * 423 * @param retain When {@code true}, the loaders aren't stopped, but, their instances 424 * are retained in a started state 425 */ doLoaderStop(boolean retain)426 public void doLoaderStop(boolean retain) { 427 mHost.doLoaderStop(retain); 428 } 429 430 /** 431 * Destroys the loaders and, if their state is not being retained, removes them. 432 */ doLoaderDestroy()433 public void doLoaderDestroy() { 434 mHost.doLoaderDestroy(); 435 } 436 437 /** 438 * Lets the loaders know the host is ready to receive notifications. 439 */ reportLoaderStart()440 public void reportLoaderStart() { 441 mHost.reportLoaderStart(); 442 } 443 444 /** 445 * Returns a list of LoaderManagers that have opted to retain their instance across 446 * configuration changes. 447 */ retainLoaderNonConfig()448 public ArrayMap<String, LoaderManager> retainLoaderNonConfig() { 449 return mHost.retainLoaderNonConfig(); 450 } 451 452 /** 453 * Restores the saved state for all LoaderManagers. The given LoaderManager list are 454 * LoaderManager instances retained across configuration changes. 455 * 456 * @see #retainLoaderNonConfig() 457 */ restoreLoaderNonConfig(ArrayMap<String, LoaderManager> loaderManagers)458 public void restoreLoaderNonConfig(ArrayMap<String, LoaderManager> loaderManagers) { 459 mHost.restoreLoaderNonConfig(loaderManagers); 460 } 461 462 /** 463 * Dumps the current state of the loaders. 464 */ dumpLoaders(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)465 public void dumpLoaders(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 466 mHost.dumpLoaders(prefix, fd, writer, args); 467 } 468 } 469