66import android .os .Bundle ;
77import android .support .annotation .AnimRes ;
88import android .support .annotation .IdRes ;
9+ import android .support .annotation .NonNull ;
10+ import android .support .annotation .Nullable ;
911import android .support .v4 .app .Fragment ;
1012import android .support .v4 .app .FragmentManager ;
1113import android .support .v4 .app .FragmentTransaction ;
2527 * Created by [email protected] on 31/05/2016. 2628 * Copyright (c) 2016 Fueled. All rights reserved.
2729 */
30+ @ SuppressWarnings ({"WeakerAccess" , "UnusedDeclaration" }) // Public API.
2831public class Flowr implements FragmentManager .OnBackStackChangedListener ,
2932 View .OnClickListener {
3033
@@ -42,11 +45,11 @@ public class Flowr implements FragmentManager.OnBackStackChangedListener,
4245 private final FragmentsResultPublisher resultPublisher ;
4346 private final int mainContainerId ;
4447
45- private FlowrScreen screen ;
46- private ToolbarHandler toolbarHandler ;
47- private DrawerHandler drawerHandler ;
48+ @ Nullable private FlowrScreen screen ;
49+ @ Nullable private ToolbarHandler toolbarHandler ;
50+ @ Nullable private DrawerHandler drawerHandler ;
4851
49- private Fragment currentFragment ;
52+ @ Nullable private Fragment currentFragment ;
5053
5154 private boolean overrideBack ;
5255 private String tagPrefix ;
@@ -61,7 +64,7 @@ public class Flowr implements FragmentManager.OnBackStackChangedListener,
6164 * @param mainContainerId the id of the container where the fragments should be displayed
6265 * @param screen the fragment's parent screen
6366 */
64- public Flowr (@ IdRes int mainContainerId , FlowrScreen screen ,
67+ public Flowr (@ IdRes int mainContainerId , @ Nullable FlowrScreen screen ,
6568 FragmentsResultPublisher resultPublisher ) {
6669 this (mainContainerId , screen , null , null , resultPublisher );
6770 }
@@ -77,7 +80,7 @@ public Flowr(@IdRes int mainContainerId, FlowrScreen screen,
7780 * @param resultPublisher the result publish to be used to publish results from fragments
7881 * that where opened for results.
7982 */
80- public Flowr (@ IdRes int mainContainerId , FlowrScreen screen , String tagPrefix ,
83+ public Flowr (@ IdRes int mainContainerId , @ Nullable FlowrScreen screen , @ NonNull String tagPrefix ,
8184 FragmentsResultPublisher resultPublisher ) {
8285 this (mainContainerId , screen , null , null , tagPrefix , resultPublisher );
8386 }
@@ -93,8 +96,9 @@ public Flowr(@IdRes int mainContainerId, FlowrScreen screen, String tagPrefix,
9396 * @param resultPublisher the result publish to be used to publish results from fragments
9497 * that where opened for results.
9598 */
96- public Flowr (@ IdRes int mainContainerId , FlowrScreen screen , ToolbarHandler toolbarHandler ,
97- DrawerHandler drawerHandler , FragmentsResultPublisher resultPublisher ) {
99+ public Flowr (@ IdRes int mainContainerId , @ Nullable FlowrScreen screen ,
100+ @ Nullable ToolbarHandler toolbarHandler , @ Nullable DrawerHandler drawerHandler ,
101+ FragmentsResultPublisher resultPublisher ) {
98102 this (mainContainerId , screen , toolbarHandler , drawerHandler , "#id-" , resultPublisher );
99103 }
100104
@@ -111,9 +115,9 @@ public Flowr(@IdRes int mainContainerId, FlowrScreen screen, ToolbarHandler tool
111115 * @param resultPublisher the result publish to be used to publish results from fragments
112116 * that where opened for results.
113117 */
114- public Flowr (@ IdRes int mainContainerId , FlowrScreen screen , ToolbarHandler toolbarHandler ,
115- DrawerHandler drawerHandler , String tagPrefix ,
116- FragmentsResultPublisher resultPublisher ) {
118+ public Flowr (@ IdRes int mainContainerId , @ Nullable FlowrScreen screen ,
119+ @ Nullable ToolbarHandler toolbarHandler , @ Nullable DrawerHandler drawerHandler ,
120+ @ NonNull String tagPrefix , FragmentsResultPublisher resultPublisher ) {
117121 this .resultPublisher = resultPublisher ;
118122 this .mainContainerId = mainContainerId ;
119123 this .tagPrefix = tagPrefix ;
@@ -148,8 +152,10 @@ public static ResultResponse getResultsResponse(Bundle arguments, int resultCode
148152
149153 Bundle requestBundle = arguments .getBundle (KEY_REQUEST_BUNDLE );
150154
151- resultResponse .fragmentId = requestBundle .getString (KEY_FRAGMENT_ID );
152- resultResponse .requestCode = requestBundle .getInt (KEY_REQUEST_CODE );
155+ if (requestBundle != null ) {
156+ resultResponse .fragmentId = requestBundle .getString (KEY_FRAGMENT_ID );
157+ resultResponse .requestCode = requestBundle .getInt (KEY_REQUEST_CODE );
158+ }
153159
154160 return resultResponse ;
155161 }
@@ -160,6 +166,7 @@ public static ResultResponse getResultsResponse(Bundle arguments, int resultCode
160166 *
161167 * @return the router screen for this router
162168 */
169+ @ Nullable
163170 protected FlowrScreen getRouterScreen () {
164171 return screen ;
165172 }
@@ -169,7 +176,7 @@ protected FlowrScreen getRouterScreen() {
169176 *
170177 * @param flowrScreen the router screen to be used
171178 */
172- public void setRouterScreen (FlowrScreen flowrScreen ) {
179+ public void setRouterScreen (@ Nullable FlowrScreen flowrScreen ) {
173180 removeCurrentRouterScreen ();
174181 if (flowrScreen != null ) {
175182 this .screen = flowrScreen ;
@@ -194,7 +201,7 @@ private void removeCurrentRouterScreen() {
194201 *
195202 * @param toolbarHandler the toolbar handler to be used.
196203 */
197- public void setToolbarHandler (ToolbarHandler toolbarHandler ) {
204+ public void setToolbarHandler (@ Nullable ToolbarHandler toolbarHandler ) {
198205 removeCurrentToolbarHandler ();
199206
200207 if (toolbarHandler != null ) {
@@ -215,7 +222,7 @@ private void removeCurrentToolbarHandler() {
215222 *
216223 * @param drawerHandler the drawer handler to be used.
217224 */
218- public void setDrawerHandler (DrawerHandler drawerHandler ) {
225+ public void setDrawerHandler (@ Nullable DrawerHandler drawerHandler ) {
219226 this .drawerHandler = drawerHandler ;
220227 }
221228
@@ -238,12 +245,16 @@ public void setDeepLinkHandlers(FlowrDeepLinkHandler... handlers) {
238245 *
239246 * @return the prefix used for the backstack fragments tag
240247 */
248+ @ NonNull
241249 protected final String getTagPrefix () {
242250 return tagPrefix ;
243251 }
244252
245253 protected <T extends Fragment & FlowrFragment > void displayFragment (TransactionData <T > data ) {
246254 try {
255+ if (screen == null ) {
256+ return ;
257+ }
247258
248259 injectDeepLinkInfo (data );
249260
@@ -337,6 +348,7 @@ private void setCustomAnimations(FragmentTransaction transaction, @AnimRes int e
337348 );
338349 }
339350
351+ @ Nullable
340352 private Fragment retrieveCurrentFragment () {
341353 Fragment fragment = null ;
342354
@@ -369,7 +381,10 @@ private void updateVisibilityState(Fragment fragment, boolean shown) {
369381 */
370382 public void close () {
371383 overrideBack = true ;
372- screen .invokeOnBackPressed ();
384+
385+ if (screen != null ) {
386+ screen .invokeOnBackPressed ();
387+ }
373388 }
374389
375390 /**
@@ -379,6 +394,10 @@ public void close() {
379394 * @param n the number of fragments to remove from the back stack
380395 */
381396 public void close (int n ) {
397+ if (screen == null ) {
398+ return ;
399+ }
400+
382401 int count = screen .getScreenFragmentManager ().getBackStackEntryCount ();
383402 if (count > 1 ) {
384403 String id = tagPrefix + (screen .getScreenFragmentManager ().getBackStackEntryCount () - n );
@@ -418,9 +437,11 @@ public void closeWithResults(ResultResponse resultResponse, int n) {
418437 * Clears the fragments back stack.
419438 */
420439 public void clearBackStack () {
421- screen .getScreenFragmentManager ()
422- .popBackStackImmediate (tagPrefix + "0" , FragmentManager .POP_BACK_STACK_INCLUSIVE );
423- currentFragment = null ;
440+ if (screen != null ) {
441+ screen .getScreenFragmentManager ()
442+ .popBackStackImmediate (tagPrefix + "0" , FragmentManager .POP_BACK_STACK_INCLUSIVE );
443+ currentFragment = null ;
444+ }
424445 }
425446
426447 /**
@@ -452,19 +473,20 @@ public void onNavigationIconClicked() {
452473 * @return true if the current fragment is the home fragment
453474 */
454475 public boolean isHomeFragment () {
455- return screen .getScreenFragmentManager ().getBackStackEntryCount () == 0 ;
476+ return screen == null || screen .getScreenFragmentManager ().getBackStackEntryCount () == 0 ;
456477 }
457478
458479 /**
459480 * Returns the fragment currently being displayed for this screen,
460481 *
461482 * @return the fragment currently being displayed
462483 */
484+ @ Nullable
463485 public Fragment getCurrentFragment () {
464486 return currentFragment ;
465487 }
466488
467- private void setCurrentFragment (Fragment newFragment ) {
489+ private void setCurrentFragment (@ Nullable Fragment newFragment ) {
468490 if (currentFragment != newFragment ) {
469491 updateVisibilityState (currentFragment , false );
470492 currentFragment = newFragment ;
@@ -486,8 +508,11 @@ public void syncScreenState() {
486508 navigationBarColor = ((FlowrFragment ) currentFragment ).getNavigationBarColor ();
487509 }
488510
489- screen .setScreenOrientation (screenOrientation );
490- screen .setNavigationBarColor (navigationBarColor );
511+ if (screen != null ) {
512+ screen .onCurrentFragmentChanged (getCurrentFragment ());
513+ screen .setScreenOrientation (screenOrientation );
514+ screen .setNavigationBarColor (navigationBarColor );
515+ }
491516
492517 syncToolbarState ();
493518 syncDrawerState ();
0 commit comments