wealthfront / magellan

The simplest navigation library for Android.
Apache License 2.0
677 stars 72 forks source link

Restarting or recreating an activity with dagger issue #45

Open mkodekar opened 7 years ago

mkodekar commented 7 years ago

For example

@Module(includes = arrayOf(SharePrefsModule::class))
class NavigatorModule {

    @Provides
    @OriggonScope
    fun provideNavigator(preferences: SharedPreferences): Navigator {
        if (preferences.getString(Utils.CRID, "").isEmpty()) {
            return Navigator.withRoot(LoginScreen()).loggingEnabled(true).build()
        } else {
            val screenList = ArrayList<Screen<*>>()
            screenList.apply {
                add(MainScreen())
                add(LikesScreen())
                add(HistoryScreen())
            }
            return Navigator.withRoot(BaseScreen(screenList)).loggingEnabled(true).build()
        }
    }
}

and the navigator.onCreate() on the injecting activity

and finally when i want to restart an activity after clearing sharedpreference , i might want to go to the loginscreen instead of the basescreen, the normal way without dependency injection works flawlessly where as the dependency injection method launches the Basescreen again.

fabiendevos commented 7 years ago

The usual way to handle that use case is to simply use rewriteHistory() in your Activity's onCreate.

Also you need to make sure that your Navigator is basically a Singleton (or at least a Singleton within your scope), and that it does not go away when the activity get's rotated.

On can simply use @Singleton and only have one Navigator for the whole app for example.