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

Whats the correct way for lazy Router? #559

Open
ursusursus opened this issue Aug 29, 2019 · 9 comments
Open

Whats the correct way for lazy Router? #559

ursusursus opened this issue Aug 29, 2019 · 9 comments

Comments

@ursusursus
Copy link

Hi, I have a layer in the UI that is only used rarely (pin code feature). I implemented it via new controller container above the "default" controller container.

Now I'd like to lazily initialize the Router, however it takes the savedInstanceState, and I presume its designed to be called in onCreate, not lazily anywhere for example in onResume

Has anyone encountered this? Is it worth avoiding at all? I'd also like to use ViewStub for best optimization however, its pointless if Router needs to be created in onCreate where savedState is accessible

@manueldidonna
Copy link

I would use the same router of the root/default container. The pin code screen could be shown as a controller with a changehandler that doesn't remove the 'from' view

@ursusursus
Copy link
Author

Not a great solution, as you need extra code to prevent somebody accidentally pushing a controller on top of pin. New router is safe I think

@dimsuz
Copy link
Contributor

dimsuz commented Aug 29, 2019

You can pass null as saved state. Our is this not an option for you as it will skip restoring a backstack?

@manueldidonna
Copy link

manueldidonna commented Aug 29, 2019

I generally use an extension function for Router to push controllers with standard rules/transitions

Maybe you can do something similar

// Implemented by every controller related to pin code feature 
interface PinCodeRoute

//  the controller must implement PinCodeRoute if the latest pushed controller did
fun Router.pushWithStandardRules(controller): Boolean

@ursusursus
Copy link
Author

You can pass null as saved state. Our is this not an option for you as it will skip restoring a backstack?

Yea exactly, if use is using the pincode and it was displayed, it wont get restored

@dimsuz
Copy link
Contributor

dimsuz commented Aug 29, 2019

We have some apps which use pin code, and we still are passing null to avoid restoring backstack, because we configure it ourselves. Basically, for pin code we store a flag between launches which is then used on app start to determine which controller should be the first one — pin code or some other one.

@ursusursus
Copy link
Author

@dimsuz right, but you still need to call Conductor.attachRouter in onCreate after rotation happened, otherwise controller will disappear from the router after rotation. Btw do you use single router or have a separate router for pincode controller?

@ursusursus
Copy link
Author

ursusursus commented Aug 30, 2019

Here is what I ended up with, using a static member, its ugly but works

private var wasPinCodeRouterInitialized = false

class Activity {

	private var pinCodeRouter: Router? = null

	onCreate(savedState) {
		if (wasPinCodeRouterInitialized) {
			pinCodeRouter = createPinCodeRouter(savedState)
		}
	}

	onResume() {
		if (someBusinessLogicOfShouldDisplayPinCode()) {
			if (pinCodeRouter == null) {
				pinCodeRouter = createPinCodeRouter(savedState)
				wasPinCodeRouterInitialized = true
			}
			pinCodeRouter.pushController(transaction(PinCodeController()))
		}
	}
	
}

@dimsuz
Copy link
Contributor

dimsuz commented Aug 30, 2019

@ursusursus we don't handle rotation, lock orientation to portrait (runs away and hides in bushes). We use a single router for the whole app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants