0

I'm using Android Navigation Architecture Component with single activity, multiple destinations. I'm initializing some of the static Variables in SplashFragment which is the "startDestination" of my NavGraph. But I'm facing problem, when app goes in background for long time (probably some memory is claimed back by Android OS), due to which some of these static variables (or Singleton Classes) got reset. When app comes in foreground, I was expecting my Activity to start with SplashFragment (start destination) again, because it is re-created (with onCreate() method called). But due to some Navigation Saving State Mechanism, it's starting from last Destination. I want to know how to clear navigation state during app Hot Start, so that it starts from Start Destination.

Vivek Vashistha
  • 832
  • 9
  • 17

2 Answers2

0

My 1st idea is to override onStop( ) method for the fragment and call navigateUp() from within, provided that you specified pop action for the destination you want to leave.

Other thing is to clear Backstack which is holding the data about your navigation history, and probably is the reason why you return to mentioned fragment and not the start destination.

MiqeyM
  • 51
  • 1
  • 6
0

I found a quick solution to this problem: During App Hot Start, Activity.onCreate() is called with "savedInstanceState" which also contains Navigation state. In my case I don't want to retain anything. Therefore I simply called super.onCreate(null) and hence not passing "savedInstanceState" variable at all.

We can also alter/clear some of the states in "savedInstanceState" variable which we don't want to restore.

Vivek Vashistha
  • 832
  • 9
  • 17