Zhuinden / simple-stack

[ACTIVE] Simple Stack, a backstack library / navigation framework for simpler navigation and state management (for fragments, views, or whatevers).
Apache License 2.0
1.36k stars 76 forks source link

`startActivityForResult` analog #218

Closed amitav13 closed 4 years ago

amitav13 commented 4 years ago

Hi, My use case is as follows:

I have a home screen (say H), some other screen(O) and a form-fill flow of 3 screens (S1, S2, S3). The form-fill flow can be started from either H or O. The end result of form-fill flow (S1 -> S2 -> S3) is a data class, say FormData. On form submission on S3, I want to pass back the FormData generated to either H or O, whichever started the flow. I'd ideally like to also pass the data between S1, S2 and S3 (assuming it is lightweight parcelable data).

Is it possible to achieve this flow using a single activity?

Zhuinden commented 4 years ago

Hi!

Thanks for the question, I do intend to answer in depth, but this is actually being discussed in https://github.com/Zhuinden/simple-stack/issues/216

If you check the latest comment https://github.com/Zhuinden/simple-stack/issues/216#issuecomment-586393688, the intended current recommendation is to have a shared scoped (implicit or explicit (ScopeKey or ScopeKey.Child)) service, and you can retrieve the parent flow service at construction time of the child service using serviceBinder.lookupService().

If you want, you can expose an interface from the child flow's service, implement it on the parent service, and register that parent service with addAlias by the name of that child interface.

That way the lookupof the child would get the interface and doesn't know exact type, only parent knows that it has to be registered as the child interface as well.

See the scoping example for more information, and ask any further questions if the explanation is unclear.


If you need a much simpler variant, you can use the same hack Google is proposing, and get the saved state associated with the previous screen's key, and stuff into its Bundle. I need to do https://github.com/Zhuinden/simple-stack/issues/217 to make that sufficiently safe to use with the default view-based navigation, though.

Zhuinden commented 4 years ago

You can check this example:

https://github.com/Zhuinden/simple-stack-tutorials/blob/1a4fb5175039f7e7ef3051491f109aa017360990/app/src/main/java/com/zhuinden/simplestacktutorials/steps/step_7/features/registration/RegistrationViewModel.kt#L12

But I can update the Repo to actually show how two flows can interact via serviceBinder.lookupService if needed.

Zhuinden commented 4 years ago

@amitav13 have any of my propositions been helpful for your use-case so far?

amitav13 commented 4 years ago

I was just checking on the feasibility of my use-case before going in on this library. Good to see that there are some potential solutions for it. Thanks!

Zhuinden commented 4 years ago

For future reference, I created a sample that shows how result passing can generally be done between screens: https://github.com/Zhuinden/simple-stack-tutorials/blob/f1ed762efe14a50dc615ebc073657056147a541c/app/src/main/java/com/zhuinden/simplestacktutorials/steps/step_8/features/form/FormViewModel.kt#L8

But the getSavedState trick would also work reliably in 2.2.5+.