-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Navigation, Saving fragment state #530
Comments
Do you really need to save fragment state while using Navigation? |
Yes, I do. It's like using FragmentTransaction.show()/hide() |
You are right. |
Can u tell more detail about it? I can imagine about it, but for working, not yet :D |
Stop imagining things! haha |
you can see this repo. |
In the case of that repo, fragments are not destroyed when navigating but they are recreate view all the time by onCreateView(). I want when I pop fragments from backstack, they are not recreate view like I do with the FragmentTransaction |
The reason fragments are recreated is using attach/detach in navigate method of KeepStateNavigator class, so you should know that the correct is use show/hide instead of attach/detach. |
yeah, yeah, I know. haha. But the point is I haven't succeed yet when I replace attach/detach by show/hide in the KeepStateNavigator class :D |
Any One solved the issue please let me know |
Did you guys manage to solve this? |
keep waiting lol |
keep waiting too !! |
Please file feature requests on the Navigation library on the issue tracker with your use case. |
Still waiting... |
|
Following...+1 |
The fix is buggy and not good enough. The legion is waiting Ian... |
I use bottom navigation which has three tabs A B and C |
same. this is crazy, cmon google! way way easier on iOS. |
the only probable solution right now is to not use Navigation unfortunately |
Currently the only working solution I found is to store the different fragment states in a shared viewmodel. Fortunately the navigation components helps us in this way with a custom Library built method called navGraphViewModel. |
could you share a piece of code of your working solution @JonathanImperato ? |
Look at this. |
I want to save fragments state. Fragments according to official used the
I have a demo you can run. |
Sounds not cool :/ |
What about NavigationView, nothing is here |
как так? непонятно.... :/ |
Ye bro)) |
Реальная дитч, ваще удобности не заметил, только что сама сетапится с некоторыми компонентами, но управлять не возможно епта)) |
You've never had huge recycler views with hundreds of items it seems. Re-inflating everything is expensive, and the recycled view pool is kept in the recycler view which gets destroyed. |
Anyone have a real solution? |
This is still buggy. I am going back to my dear Fragment Manager and Transactions. |
If someone is working with Bottom Navigation, and try to save state(well in fact just load all fragments on create of host activity and hide/show) then, you can follow this: https://github.com/bukunmialuko/BottomNavViewWithFragments We are using this in production. Here are some issues with this,
|
@ianhanniballake I would like to keep a view state + its view model in scenarios where there is an infinite scroll recycler view that opens up a details view, it makes much more sense to just push a new fragment, when a user wants to see details view, without losing recycler view's state (scroll position). Manually storing the recyclerview's state is tricky because for any navigation that is not a details view I don't want to keep the recyclerview's state, but for details navigation I do. |
After 1.5 years, this still can not be done. LOL |
waiting... |
https://issuetracker.google.com/issues/127932815 🔥 They won’t fix this issue because it's an Intended Behavior. Another improvement ticket was created and accepted: https://issuetracker.google.com/issues/80029773 |
I suggest that google developers add this limitation at the beginning of their navigation component document to warn those who want to use this library (in a hope that it will make their life easier) to realize this behaviour because it is a real showstopper for most people (come on, this use case where a user navigate off the long-list page and come back is extremely common). I know there is an ugly workaround for this (saving state) but it is obviously inefficient and hard. The thing is, If I had known this limitation, I would have surely not invested a lot of my time implementing my navigation based on this library because ,as it stands, sorry to say this but it is useless and it is more trouble that it's worth. Google should have hired the developers with good mindset who intend to do things that makes other developer's lives easier, especially considering this is the Androidx library whose intention is to encourage developers to stay on Android development (let's face it, Android development is hard, people are moving away, and that's why your team was formed in the first place to make Android development more attractive and here's what happens...). Sorry to be a bit offensive but I just spoke my mind and I hope it gets heard. |
Same problem here, with version 2.3.0 that I just updated today. Anyone with some workaround to bypass this? |
I used findNavController().popBackStack() to show my previous fragment which is a list and it keeps the state intact. |
Do you like seeing your screen flick when you navigate back as a user?! No, right. So yeah there gotta be a better solution than just reinflating the whole mess. |
When your project has been developed to close to completion, and then you find that it is so difficult to implement such a simple requirement, it is a disaster |
I have deleted all Navigation Graph library from my project and I did as always: Using Activities. |
I regret the day I tried the navigation component. Still have about 5 big projects running with navigation, which I am reluctant to refactor, but you give me no choice. After 1.5 year and a lot a bugs and workarounds, I'm going back to activities... |
that kotlin workaround file (until navigation 2.4.0) has bugs when implemented on java codes, at runtime. why this is closed? |
i would recommend you to use single activity pattern and cicerony library for navigation. You can watch about single activity in cicerony's author presentations |
still waiting... A favorable way should be that when doing the navigation, if I pass a boolean-type parameter, for example "resuse", the fragment should save its state (by default not save). |
I found a very easy way which can make only your home fragment constant through whole activity using back press. But it only works if you set up your navigation to have a home fragment, and every other fragment goes to home fragment on the back press. It works well for GoogleMaps, because it takes a second to load them which makes the whole thing ugly. |
Hey everyone that is still encountering problems. I have found a workaround which is most likely a classic duct-tape fix. Fiddled around with some .show and switching within the navigation buttons I have located in my project. Have a look at https://github.com/yassirlaaouissi/IKPMD/blob/master/app/src/main/java/com/example/ikpmd_periode2/MainActivity.java A small overview of what I did:
|
You can override FragmentFactory class like this:
And apply factory in main activity:
You can implements your own interface SingleFragment to fragments to adding only its to HashMap and other will be work like before. |
Okay you win bro |
Any updates on this, preferably from the Android team? The hacks and workarounds aren't working for me and I'm thinking of reverting everything. But it's such a pain to do that and use 'old navigation' again when this solution is so elegant and perfect, except for the fact that Fragment states aren't saved. Still a fan of @cumtsx idea of adding a Boolean type attribute like |
The new navigation library is a mess, they have destroyed the standard way the things were working, you expect to load a fragment, you expect if u go to another fragment and go back, to see the prev fragment as it was when u left it, NOT being created again from scratch every single time u change to another fragment or at least have easy control of it. But not with the new nav arch, there is always an issue and workaround for everything you would want to do or another library you are proposed to use cuz google want u to. Issues with this like state, using the same fragments in nav or arguments. There is even not a single sample about creating navigation programmatically, they except devs that have apps with like couple of dozens menu items to do it manually or what, the documentation is lacking a lot. U can do everything, but not with the help of google. Is this some kind of hidden forced android app optimization so they use less memory at the cost for recreating everything again and again??? As this is how it looks, complete madness. Looks like it is way way better to just stick with managing everything yourself with custom code, it will take you longer at first, but after you have a working solution just as it was before this atrocious new component it will just work as expected and u will have 100% control over it instead of wasting time looking for some workarounds. |
This issue was mentioned in the Compose reddit AMA, but it appears that comment never made it back here, so I'll include the reply in full here to the question "must I really re-inflate the view EVERY time I go back to my fragment having to manually keep the state of everything in check, or can I just retain the view and save all the hassle? Is retaining fragment views okay, is it REALLY a memory leak? Why must the nav component limit developer options by not allowing them to retain the view and use the simple hide()/show()(instead of replace()) methods of the Fragment API instead of having to re-inflate the view every time(inflations are a heavy operation).":
I'll add to this that since then, implementation work on multiple back stacks has begun:
This multiple back stack work is exactly what is needed to save and restore the state of fragments when swapping between bottom navigation items, for instance. |
Hi, I'm was trying to save fragment state, Is there a suggested way to use while using Navigation component?
It's like open new fragment without lose the previous fragment states.
The text was updated successfully, but these errors were encountered: