Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GlobalServices.Factory receive the Backstack as a parameter in create() #231

Closed
Zhuinden opened this issue Jul 2, 2020 · 0 comments
Milestone

Comments

@Zhuinden
Copy link
Owner

Zhuinden commented Jul 2, 2020

Reported on Slack, Navigator has a design oversight that requires passing in a class that has a lateinit var backstack: Backstack field into the GlobalServices.Factory in order to pass the Backstack to the factory, and therefore to lazy-initialized GlobalServices.

Unfortunately, GlobalServices.Factory's create() method has no args, and so changing this contract is a breaking change. I wasn't really planning on a 3.0.0 release at this time, so for now, I'll just post what you can do to resolve this conundrum.

Option 1:

Ditch Navigator, and instantiate Backstack manually. Keep it across config changes, and invoke the right methods (refer to tutorial step 1).

In this case however, passing backstack as a parameter to things is the responsibility of the library user. It is theoretically meant to be an ActivityRetainedScope'd object, which Hilt does support out of the box, but lifecycle integration is still required.

Option 2: (recommended):

class CustomGlobalServicesFactory: GlobalServices.Factory {
    lateinit var backstack: Backstack

    override fun create(): GlobalServices = 
        GlobalServices.builder()
            .add(SomeService(backstack)) // backstack is definitely set when this is called!
            // don't add the `backstack` as a global service
            .build()
}

And now you can do:

val globalServicesFactory = CustomGlobalServicesFactory()

val backstack = Navigator.configure()
           .setDeferredInitialization(true)
           .setGlobalServices(globalServicesFactory)
           .setStateChanger(SimpleStateChanger(this))
           .install(this, container, History.of(FirstScreen())`

globalServicesFactory.backstack = backstack

Navigator.executeDeferredInitialization(this)
@Zhuinden Zhuinden added this to the 3.0 milestone Jul 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant