ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

Show/Hide fragment #86

Closed adibon closed 6 years ago

adibon commented 7 years ago

Hello, I need to keep the state of fragments when switching between tabs and not reload them every time. Would it be possible to replace attach() in reattachPreviousFragment() with show() and replace detach() in detachCurrentFragment() with hide()?

could you add an option to choose whether to attach/detach or show/hide fragment? Thanks

ajaystha005 commented 7 years ago

Hello, Is there is any way to prevent fragment from loading when switching between tabs?

Thank you

finneapps commented 7 years ago

I am trying to develop an app with a single Activity using this lib together with a bottom bar (iOS like navigation 👎 ) and I found it very hard.

The attach/detach approach is very hard to get right. Imagine you have a Fragment and then select a button that opens a new Fragment with a ViewPager with 5 tabs each containing a RecyclerView. Then you scroll down on one of the RecycleViews and then you press an item and a new Fragment is shown, the previous Fragment with the ViewPager is detached. Then you press back, and the Fragment with the ViewPager is the Attached again and all content needs to be reloaded and the ScrollPositions needs to be restored. You could also imagine the same example again but before pressing back, you would rotate the device, this also gives you some "interesting" challenges.

But one problem with the show/hide approach is that you don't get any lifecycle events. So when you hide a Fragment, you have to invent your own event to let the Fragment know that it should react on certain stuff...

I haven't really found any good information about how to make an Android app with a single Activity and multiple Fragments. Seems that the Android Framework does not really support it in a nice way

adibon commented 7 years ago

Hi @finneapps, It's your Activity that should handle all the navigation. Everytime you need to show a new fragment, like in your example when a user select an item, you needs to call the "pushFragment" method from the activity. So that the current fragment with Viewpager will hide and not be detached. The next time you press back on the new fragment, the viewpager Fragment will show again with the same state.

I agree that losing the fragment Lifecycle is not good. I am using one ViewModel and LiveData to refresh the fragments based on events. And I am using onHiddenChanged(boolean hidden) in the fragment that acts pretty much like onResume()/onPause().

finneapps commented 7 years ago

I am using the Activity for the navigation. If you look at the code for the pushFragment method you will see that detachCurrentFragment is called which detaches the Fragment. Yes, the use of Show/Hide would keep the Fragment in the same state without the need for recreating

adibon commented 7 years ago

ah ok I misread sorry, I thought you had an issue with show/hide not attach/detach in your example.

adibon commented 7 years ago

If you use this hide/show fragment you may encounter the exception "IllegalStateException when replacing a Fragment". Adding ft.detach(fragment) before fr.remove(fragment) seems to fix it. like in the method clearFragmentManager()

I haven't had any other issues and my app is working fine

ranjitzade commented 6 years ago

@finneapps Can you please explain how you solved the viewpager issue with this library?

VickyDonor36 commented 6 years ago

I am using your library... on going from fragment A to B and then coming back from B to A, Fragment A is loading again, How to handle it so that it should not reload the Fragment A again, is should look like as if it the fragment has come from the back stack...please let me know if you have something

jishhd commented 6 years ago

Is there any solution for this, @ncapdevi ? My root fragments consistently get reloaded when returning to them, even though I can tell they still have stored values from the previous time they were in focus. This seems like such a strange oversight. We shouldn't have to recreate fragments each time, it kind of defeats the purpose no?

ncapdevi commented 6 years ago

This is standard Android procedure. Fragments have their own lifecycles, and thus need to have their own state management. https://developer.android.com/guide/components/fragments.html#Lifecycle . It has already been determine that hide/show is not the ideal long term solution for what is far and away the most common use-case, and handling restoration of state should be required regardless of wether you're using this library or not. http://daniel-codes.blogspot.com/2012/06/fragment-transactions-reference.html