Closed ibrahimAlii closed 5 years ago
Do you really need to save fragment state while using Navigation?
Yes, I do. It's like using FragmentTransaction.show()/hide()
Yes, I do. It's like using FragmentTransaction.show()/hide()
You are right. Using FragmentTransaction.add()/show()/hide() by override the method navigate() of FragmentNavigator
You are right. Using FragmentTransaction.add()/show()/hide() by override the method navigate() of FragmentNavigator
Can u tell more detail about it? I can imagine about it, but for working, not yet :D
Stop imagining things! haha
You are right. Using FragmentTransaction.add()/show()/hide() by override the method navigate() of FragmentNavigator
Can u tell more detail about it? I can imagine about it, but for working, not yet :D
you can see this repo. https://github.com/STAR-ZERO/navigation-keep-fragment-sample
You are right. Using FragmentTransaction.add()/show()/hide() by override the method navigate() of FragmentNavigator
Can u tell more detail about it? I can imagine about it, but for working, not yet :D
you can see this repo. https://github.com/STAR-ZERO/navigation-keep-fragment-sample
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
You are right. Using FragmentTransaction.add()/show()/hide() by override the method navigate() of FragmentNavigator
Can u tell more detail about it? I can imagine about it, but for working, not yet :D
you can see this repo. https://github.com/STAR-ZERO/navigation-keep-fragment-sample
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.
You are right. Using FragmentTransaction.add()/show()/hide() by override the method navigate() of FragmentNavigator
Can u tell more detail about it? I can imagine about it, but for working, not yet :D
you can see this repo. https://github.com/STAR-ZERO/navigation-keep-fragment-sample
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...
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 when i go from tab A to Tab B then i revisited to tab A it recreates the tab A i want to save fragment state dont want to recreate the fragment A.
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 ?
could you share a piece of code of your working solution @JonathanImperato ?
Look at this. Just use it like a normal ViewModel that is global to every fragment available in the navigation graph.
I want to save fragments state. Fragments according to official used the replace()
method that doesn't save fragment state, but we expect to use the add()/hide()
methods shows fragments to save fragment state. My solution is to not use the official implementation implementation "androidx.navigation:navigation-fragment:$nav_version"
, directly put the library's code in your project, and then modify the replace()
method to hide()/add()
method.
public NavDestination navigate(@NonNull Destination destination, @Nullable Bundle args,
@Nullable NavOptions navOptions, @Nullable Navigator.Extras navigatorExtras) {
// ft.replace(mContainerId, frag);
// change to
if(mFragmentManager.getFragments().size()>0){
ft.hide(mFragmentManager.getFragments().get(mFragmentManager.getFragments().size()-1));
ft.add(mContainerId, frag);
}else {
ft.replace(mContainerId, frag);
}
}
I have a demo you can run. fragment-demo
/**
* Manages the various graphs needed for a [BottomNavigationView].
*
* This sample is a workaround until the Navigation Component supports multiple back stacks.
*/
Sounds not cool :/
What about NavigationView, nothing is here
Did Google solve the retain instance problem?
We are still waiting! :D
We are still waiting! :D
I want to save fragments state. Fragments according to official used the
replace()
method that doesn't save fragment state, but we expect to use theadd()/hide()
methods shows fragments to save fragment state. My solution is to not use the official implementationimplementation "androidx.navigation:navigation-fragment:$nav_version"
, directly put the library's code in your project, and then modify thereplace()
method tohide()/add()
method.
- androidx.navigation.fragment.FragmentNavigator.java
public NavDestination navigate(@NonNull Destination destination, @Nullable Bundle args, @Nullable NavOptions navOptions, @Nullable Navigator.Extras navigatorExtras) { // ft.replace(mContainerId, frag); // change to if(mFragmentManager.getFragments().size()>0){ ft.hide(mFragmentManager.getFragments().get(mFragmentManager.getFragments().size()-1)); ft.add(mContainerId, frag); }else { ft.replace(mContainerId, frag); } }
I have a demo you can run. fragment-demo
Sound good, we can solve the problem this way. But copy all related source codes then modify one line of codes is not a good behavior. I'm also trying to extends FragmentNavigator but can not change super()'s logic. Google should open it for us can customize
We are still waiting! :|
We are still waiting! :|
We are still waiting! :|
i guess we are still waiting :)
We are still waiting! :|
We are still waiting! :|
We are still waiting! :|
I found a work around, the key is to create your own NavHostFragment and custom fragment navigator and set it inside your custom NavHostFragment.
i used code from from nav component library and made edit for adding and hiding fragments.
https://gist.github.com/farhanahmed95/a33aaabdb4fb602d515005c34e864ffe
Reuse the View
can fix the problem in most cases:
abstract class DataBindingFragment<T : ViewDataBinding> : Fragment() {
lateinit var binding: T
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
if (!::binding.isInitialized) {
binding = onCreateDataBinding(
inflater,
container,
savedInstanceState
)
}
return binding.root
}
abstract fun onCreateDataBinding(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): T
}
Any solution or optimized work around?? We are still waiting !!
come on!!! I don't want to send the same request every time the user tapped the bottom navigation bar!!! solve it please! every time when we tap the bottom navigation view fragment item it creates the whole fragment again! what is the point??? you make us regret to use new components!!!
I update the navigation extensions class and the state problem has fixed:
https://gist.github.com/theozgurr/506895a4f0f820a6c60e97eb6026198b
anybody out there?! google!
Why is this closed it's not fixed ffs? What a joke
Yeah... the navigation component has been my least favorite androidx library. I am guessing they are working on a complete revamp judging by the silence so far.
Should have never been moved out of beta given the limitations (no fragment reuse being among them).
I update the navigation extensions class and the state problem has fixed:
https://gist.github.com/theozgurr/506895a4f0f820a6c60e97eb6026198b
It is not working with <dialog tags, it is crashing :(
I update the navigation extensions class and the state problem has fixed: https://gist.github.com/theozgurr/506895a4f0f820a6c60e97eb6026198b
It is not working with <dialog tags, it is crashing :(
did you mean dialog fragments?
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.