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 75 forks source link

Multistack with Fragment host #237

Closed uvaysss closed 3 years ago

uvaysss commented 3 years ago

Hi, thank your for this library!

I'm currently trying to implement single activity navigation on fragments, but having trouble with a multistack screen. In the sample the host for multistack is an Activity, but I need it for a fragment. The problem is that I don't know what the fragment alternative would be for onRetainCustomNonConfigurationInstance() and getSystemService().

Maybe you could give a hint?

Zhuinden commented 3 years ago

Quick question: so you have multiple fragments, only one of them has 2+ bottom tabs on which BottomNavigationView should be available, and each tab requires its own history management (so isn't just 1 root fragment per tab)?

uvaysss commented 3 years ago

Correct. I have a parent flow of fullscreen fragments and then 1 fragment will contain tabs with their own stack of fragments.

Zhuinden commented 3 years ago

@uvaysss I added a sample that shows how you can create a multi-stack inside a fragment using nested backstacks.

https://github.com/Zhuinden/simple-stack/commit/c64bd3a2f8a56ba765392bc0b195727a1b7188f3 😉

Zhuinden commented 3 years ago

Did that help?

uvaysss commented 3 years ago

Wow, that was quick! Thank a lot for the sample! I didn't have time to checkout the sample yet. I will give you a feedback later. Thank you again!

Zhuinden commented 3 years ago

As the fact that this sample exists now (and is, as far as I'm aware, a complete solution to the multistack problem) is a pretty big deal, I'll have an article up here explaining how it works: https://medium.com/@Zhuinden/creating-a-bottomnavigation-multi-stack-using-child-fragments-with-simple-stack-c73c1ca3bbd4

uvaysss commented 3 years ago

Did a quick checkout of the sample and already implemented in a demo app. So far it just works. Thank you!

But I was wondering what would be the right solution to "fix" the fragment view recreation problem. Well I could replace the attach and detach methods with show and hide, but I'm not sure if in this case it's okay to do so. Maybe just keep a reference to the created view.

Zhuinden commented 3 years ago

You can override isNotShowing, showFragment, hideFragment in DefaultFragmentStateChanger to use show/hide instead of attach/detach, a key difference though is that hidden fragments don't become stopped.

Wait, you mean for the FragmentStackHostFragments. Yes, you can replace the detach/attach with hide/show. Same principles for not being stopped apply, you might need to also override onHiddenChanged to decide that the fragment isn't available for handling back.

You can use setMaxLifecycle(STARTED) to make onPause work

uvaysss commented 3 years ago

Did not really get the part with setMaxLifecycle(STARTED), but with show/hide and just onHiddenChanged is working fine.

Also, would be great to hear your opinion about the "solution" of fragment view recreation with holding a reference to that view.

Zhuinden commented 3 years ago

I hear retaining the view despite the Fragment expecting it to be dead can break things so I'd advise against doing that.

But I'm not entirely sure on the exact issues that can arise.

I mostly just mentioned setMaxLifecycle because the code for FragmentStackHostFragment was using onResume/onPause to decide if it's eligible for back, the benefit to that is that setMaxLifecycle(STARTED) (and of course, RESUMED with show) combined with hide/show can eliminate the need for using the separate onHiddenChanged.