blob: 9ea15a04e150d887f1e08d7a8ff20f65119e9d6c [file] [log] [blame]
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001/*
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
17package android.app;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.content.res.Configuration;
22import android.os.Bundle;
23import android.os.Parcelable;
24import android.util.ArrayMap;
25import android.util.AttributeSet;
26import android.view.Menu;
27import android.view.MenuInflater;
28import android.view.MenuItem;
29import android.view.View;
30
31import java.io.FileDescriptor;
32import java.io.PrintWriter;
33import java.util.List;
34
35/**
36 * Provides integration points with a {@link FragmentManager} for a fragment host.
37 * <p>
38 * It is the responsibility of the host to take care of the Fragment's lifecycle.
39 * The methods provided by {@link FragmentController} are for that purpose.
40 */
41public class FragmentController {
42 private final FragmentHostCallback<?> mHost;
43
44 /**
45 * Returns a {@link FragmentController}.
46 */
47 public static final FragmentController createController(FragmentHostCallback<?> callbacks) {
48 return new FragmentController(callbacks);
49 }
50
51 private FragmentController(FragmentHostCallback<?> callbacks) {
52 mHost = callbacks;
53 }
54
55 /**
56 * Returns a {@link FragmentManager} for this controller.
57 */
58 public FragmentManager getFragmentManager() {
59 return mHost.getFragmentManagerImpl();
60 }
61
62 /**
63 * Returns a {@link LoaderManager}.
64 */
65 public LoaderManager getLoaderManager() {
66 return mHost.getLoaderManagerImpl();
67 }
68
69 /**
70 * Returns a fragment with the given identifier.
71 */
72 @Nullable
73 public Fragment findFragmentByWho(String who) {
74 return mHost.mFragmentManager.findFragmentByWho(who);
75 }
76
77 /**
78 * Attaches the host to the FragmentManager for this controller. The host must be
79 * attached before the FragmentManager can be used to manage Fragments.
80 * */
81 public void attachHost(Fragment parent) {
82 mHost.mFragmentManager.attachController(
83 mHost, mHost /*container*/, parent);
84 }
85
86 /**
87 * Instantiates a Fragment's view.
88 *
89 * @param parent The parent that the created view will be placed
90 * in; <em>note that this may be null</em>.
91 * @param name Tag name to be inflated.
92 * @param context The context the view is being created in.
93 * @param attrs Inflation attributes as specified in XML file.
94 *
95 * @return view the newly created view
96 */
97 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
98 return mHost.mFragmentManager.onCreateView(parent, name, context, attrs);
99 }
100
101 /**
102 * Marks the fragment state as unsaved. This allows for "state loss" detection.
103 */
104 public void noteStateNotSaved() {
105 mHost.mFragmentManager.noteStateNotSaved();
106 }
107
108 /**
109 * Saves the state for all Fragments.
110 */
111 public Parcelable saveAllState() {
112 return mHost.mFragmentManager.saveAllState();
113 }
114
115 /**
116 * Restores the saved state for all Fragments. The given Fragment list are Fragment
117 * instances retained across configuration changes.
118 *
119 * @see #retainNonConfig()
Adam Powell44ba79e2016-02-04 16:20:37 -0800120 *
121 * @deprecated use {@link #restoreAllState(Parcelable, FragmentManagerNonConfig)}
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700122 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700123 @Deprecated
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700124 public void restoreAllState(Parcelable state, List<Fragment> nonConfigList) {
Adam Powell44ba79e2016-02-04 16:20:37 -0800125 mHost.mFragmentManager.restoreAllState(state,
126 new FragmentManagerNonConfig(nonConfigList, null));
127 }
128
129 /**
130 * Restores the saved state for all Fragments. The given FragmentManagerNonConfig are Fragment
131 * instances retained across configuration changes, including nested fragments
132 *
133 * @see #retainNestedNonConfig()
134 */
135 public void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) {
136 mHost.mFragmentManager.restoreAllState(state, nonConfig);
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700137 }
138
139 /**
140 * Returns a list of Fragments that have opted to retain their instance across
141 * configuration changes.
Adam Powell44ba79e2016-02-04 16:20:37 -0800142 *
143 * @deprecated use {@link #retainNestedNonConfig()} to also track retained
144 * nested child fragments
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700145 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700146 @Deprecated
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700147 public List<Fragment> retainNonConfig() {
Adam Powell44ba79e2016-02-04 16:20:37 -0800148 return mHost.mFragmentManager.retainNonConfig().getFragments();
149 }
150
151 /**
152 * Returns a nested tree of Fragments that have opted to retain their instance across
153 * configuration changes.
154 */
155 public FragmentManagerNonConfig retainNestedNonConfig() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700156 return mHost.mFragmentManager.retainNonConfig();
157 }
158
159 /**
160 * Moves all Fragments managed by the controller's FragmentManager
161 * into the create state.
162 * <p>Call when Fragments should be created.
163 *
164 * @see Fragment#onCreate(Bundle)
165 */
166 public void dispatchCreate() {
167 mHost.mFragmentManager.dispatchCreate();
168 }
169
170 /**
171 * Moves all Fragments managed by the controller's FragmentManager
172 * into the activity created state.
173 * <p>Call when Fragments should be informed their host has been created.
174 *
175 * @see Fragment#onActivityCreated(Bundle)
176 */
177 public void dispatchActivityCreated() {
178 mHost.mFragmentManager.dispatchActivityCreated();
179 }
180
181 /**
182 * Moves all Fragments managed by the controller's FragmentManager
183 * into the start state.
184 * <p>Call when Fragments should be started.
185 *
186 * @see Fragment#onStart()
187 */
188 public void dispatchStart() {
189 mHost.mFragmentManager.dispatchStart();
190 }
191
192 /**
193 * Moves all Fragments managed by the controller's FragmentManager
194 * into the resume state.
195 * <p>Call when Fragments should be resumed.
196 *
197 * @see Fragment#onResume()
198 */
199 public void dispatchResume() {
200 mHost.mFragmentManager.dispatchResume();
201 }
202
203 /**
204 * Moves all Fragments managed by the controller's FragmentManager
205 * into the pause state.
206 * <p>Call when Fragments should be paused.
207 *
208 * @see Fragment#onPause()
209 */
210 public void dispatchPause() {
211 mHost.mFragmentManager.dispatchPause();
212 }
213
214 /**
215 * Moves all Fragments managed by the controller's FragmentManager
216 * into the stop state.
217 * <p>Call when Fragments should be stopped.
218 *
219 * @see Fragment#onStop()
220 */
221 public void dispatchStop() {
222 mHost.mFragmentManager.dispatchStop();
223 }
224
225 /**
226 * Moves all Fragments managed by the controller's FragmentManager
227 * into the destroy view state.
228 * <p>Call when the Fragment's views should be destroyed.
229 *
230 * @see Fragment#onDestroyView()
231 */
232 public void dispatchDestroyView() {
233 mHost.mFragmentManager.dispatchDestroyView();
234 }
235
236 /**
237 * Moves all Fragments managed by the controller's FragmentManager
238 * into the destroy state.
239 * <p>Call when Fragments should be destroyed.
240 *
241 * @see Fragment#onDestroy()
242 */
243 public void dispatchDestroy() {
244 mHost.mFragmentManager.dispatchDestroy();
245 }
246
247 /**
Wale Ogunwale7c796812016-01-29 21:13:50 -0800248 * Lets all Fragments managed by the controller's FragmentManager know the multi-window mode of
249 * the activity changed.
250 * <p>Call when the multi-window mode of the activity changed.
251 *
Andrii Kulian933076d2016-03-29 17:04:42 -0700252 * @see Fragment#onMultiWindowModeChanged
Wale Ogunwale7c796812016-01-29 21:13:50 -0800253 */
Andrii Kulian933076d2016-03-29 17:04:42 -0700254 public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) {
255 mHost.mFragmentManager.dispatchMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -0800256 }
257
258 /**
259 * Lets all Fragments managed by the controller's FragmentManager know the picture-in-picture
260 * mode of the activity changed.
261 * <p>Call when the picture-in-picture mode of the activity changed.
262 *
Andrii Kulian933076d2016-03-29 17:04:42 -0700263 * @see Fragment#onPictureInPictureModeChanged
Wale Ogunwale7c796812016-01-29 21:13:50 -0800264 */
Andrii Kulian933076d2016-03-29 17:04:42 -0700265 public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
266 mHost.mFragmentManager.dispatchPictureInPictureModeChanged(isInPictureInPictureMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -0800267 }
268
269 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700270 * Lets all Fragments managed by the controller's FragmentManager
271 * know a configuration change occurred.
272 * <p>Call when there is a configuration change.
273 *
274 * @see Fragment#onConfigurationChanged(Configuration)
275 */
276 public void dispatchConfigurationChanged(Configuration newConfig) {
277 mHost.mFragmentManager.dispatchConfigurationChanged(newConfig);
278 }
279
280 /**
281 * Lets all Fragments managed by the controller's FragmentManager
282 * know the device is in a low memory condition.
283 * <p>Call when the device is low on memory and Fragment's should trim
284 * their memory usage.
285 *
286 * @see Fragment#onLowMemory()
287 */
288 public void dispatchLowMemory() {
289 mHost.mFragmentManager.dispatchLowMemory();
290 }
291
292 /**
293 * Lets all Fragments managed by the controller's FragmentManager
294 * know they should trim their memory usage.
295 * <p>Call when the Fragment can release allocated memory [such as if
296 * the Fragment is in the background].
297 *
298 * @see Fragment#onTrimMemory(int)
299 */
300 public void dispatchTrimMemory(int level) {
301 mHost.mFragmentManager.dispatchTrimMemory(level);
302 }
303
304 /**
305 * Lets all Fragments managed by the controller's FragmentManager
306 * know they should create an options menu.
307 * <p>Call when the Fragment should create an options menu.
308 *
309 * @return {@code true} if the options menu contains items to display
310 * @see Fragment#onCreateOptionsMenu(Menu, MenuInflater)
311 */
312 public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
313 return mHost.mFragmentManager.dispatchCreateOptionsMenu(menu, inflater);
314 }
315
316 /**
317 * Lets all Fragments managed by the controller's FragmentManager
318 * know they should prepare their options menu for display.
319 * <p>Call immediately before displaying the Fragment's options menu.
320 *
321 * @return {@code true} if the options menu contains items to display
322 * @see Fragment#onPrepareOptionsMenu(Menu)
323 */
324 public boolean dispatchPrepareOptionsMenu(Menu menu) {
325 return mHost.mFragmentManager.dispatchPrepareOptionsMenu(menu);
326 }
327
328 /**
329 * Sends an option item selection event to the Fragments managed by the
330 * controller's FragmentManager. Once the event has been consumed,
331 * no additional handling will be performed.
332 * <p>Call immediately after an options menu item has been selected
333 *
334 * @return {@code true} if the options menu selection event was consumed
335 * @see Fragment#onOptionsItemSelected(MenuItem)
336 */
337 public boolean dispatchOptionsItemSelected(MenuItem item) {
338 return mHost.mFragmentManager.dispatchOptionsItemSelected(item);
339 }
340
341 /**
342 * Sends a context item selection event to the Fragments managed by the
343 * controller's FragmentManager. Once the event has been consumed,
344 * no additional handling will be performed.
345 * <p>Call immediately after an options menu item has been selected
346 *
347 * @return {@code true} if the context menu selection event was consumed
348 * @see Fragment#onContextItemSelected(MenuItem)
349 */
350 public boolean dispatchContextItemSelected(MenuItem item) {
351 return mHost.mFragmentManager.dispatchContextItemSelected(item);
352 }
353
354 /**
355 * Lets all Fragments managed by the controller's FragmentManager
356 * know their options menu has closed.
357 * <p>Call immediately after closing the Fragment's options menu.
358 *
359 * @see Fragment#onOptionsMenuClosed(Menu)
360 */
361 public void dispatchOptionsMenuClosed(Menu menu) {
362 mHost.mFragmentManager.dispatchOptionsMenuClosed(menu);
363 }
364
365 /**
366 * Execute any pending actions for the Fragments managed by the
367 * controller's FragmentManager.
368 * <p>Call when queued actions can be performed [eg when the
369 * Fragment moves into a start or resume state].
370 * @return {@code true} if queued actions were performed
371 */
372 public boolean execPendingActions() {
373 return mHost.mFragmentManager.execPendingActions();
374 }
375
376 /**
377 * Starts the loaders.
378 */
379 public void doLoaderStart() {
380 mHost.doLoaderStart();
381 }
382
383 /**
384 * Stops the loaders, optionally retaining their state. This is useful for keeping the
385 * loader state across configuration changes.
386 *
387 * @param retain When {@code true}, the loaders aren't stopped, but, their instances
388 * are retained in a started state
389 */
390 public void doLoaderStop(boolean retain) {
391 mHost.doLoaderStop(retain);
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700392 }
393
394 /**
395 * Destroys the loaders and, if their state is not being retained, removes them.
396 */
397 public void doLoaderDestroy() {
398 mHost.doLoaderDestroy();
399 }
400
401 /**
402 * Lets the loaders know the host is ready to receive notifications.
403 */
404 public void reportLoaderStart() {
405 mHost.reportLoaderStart();
406 }
407
408 /**
409 * Returns a list of LoaderManagers that have opted to retain their instance across
410 * configuration changes.
411 */
412 public ArrayMap<String, LoaderManager> retainLoaderNonConfig() {
413 return mHost.retainLoaderNonConfig();
414 }
415
416 /**
417 * Restores the saved state for all LoaderManagers. The given LoaderManager list are
418 * LoaderManager instances retained across configuration changes.
419 *
420 * @see #retainLoaderNonConfig()
421 */
422 public void restoreLoaderNonConfig(ArrayMap<String, LoaderManager> loaderManagers) {
423 mHost.restoreLoaderNonConfig(loaderManagers);
424 }
425
426 /**
427 * Dumps the current state of the loaders.
428 */
429 public void dumpLoaders(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
430 mHost.dumpLoaders(prefix, fd, writer, args);
431 }
432}