I'm updating the old navigation system from my app to jetpack navigation and I'm facing an issue:
From a DialogFragment which can be displayed from almost any fragment in the app, I have a button which leads to a specific Fragment. I've found in the documentation global actions, but it seems they clear the entire navigation backstack when you navigate with one of these and I need to keep my backstack, just add this new fragment to it from anywhere. What's the solution to this problem ?
Thanks
Related
My problem is illustrated by this screen record.
When I click the FAB on FinanceFragment, it will open AddTransactionFragment. But when I navigate to ReminderFragment and go back to FinanceFragment, the AddTransactionFragment is destroyed.
How could I -- similar to the YouTube app when one switches between fragments -- keep AddTransactionFragment visible while I navigate to another fragment?
I tried to add addToBackStack() but this turned out not to be what I was looking for.
I have the following navigation xml on a BottomNavigationView, however when the user navigates inside the 1st fragment and then navigates to another fragment using the BottomNavigationView and then comes back to the initial fragment, it resets all the way to the default 1st fragment the navigation to a deeper fragment does not persist, i assume this is because the fragment gets recreated and from what i've looked at on other questions is that there is a way to save the data to re-set any text values on text fields or similar stuff, however i haven't seen if theres a way to avoid the fragment from being destroyed and re-created in order to make the navigation and whatever changes the user had previously made to the view persist.
Is there a way to do that, or should i use a savedInstance in such a way that it re-navigates to the previously navigated fragment?
Part of Navigation 2.4.0 is support for multiple back stacks - i.e., the ability for each tab of a bottom navigation bar to save its state, restoring that state when you reselect that tab.
As per that blog post:
If you’re using NavigationUI, our set of opinionated helpers for connecting your NavController to Material view components, you’ll find that multiple back stacks is enabled by default for menu items, BottomNavigationView (and now NavigationRailView!), and NavigationView. This means that the common combination of using navigation-fragment and navigation-ui will just work.
This means that if you are using the setupWithNavController methods, then upgrading to Navigation 2.4 will give you support for multiple back stacks immediately. You can verify that by going to your order fragment (thus building a back stack on that first tab), going to a different tab, then tap on the first tab again to reselect it.
Of course, it is your fragments state, not the instances themselves that are saved and restored. This means that each individual fragment must still save its state properly.
I hava two Fragment(OverviewFragment、PersonalFragment), and one sub graph(contains two Fragment :BucketsFragment and ObjectsFragment, BucketsFragment is start destination). They are bind to one BottomNavigateView.
Now, After navigate to the sub graph start destination(BucketsFragment), and continue to navigate to ObjectsFragment. Then navigate to OverviewFragment and finally return to the sub graph.
Now, I came to start destination(BucketsFragment), but actually what I want is ObjectsFragment. what should I do?
The whole process is shown here
I want to navigate to the sub graph that retains the previous state, instead of a new sub graph.
As per the material design guidelines for bottom navigation:
On Android: the app navigates to a destination’s top-level screen. Any prior user interactions and temporary screen states are reset, such as scroll position, tab selection, and in-line search.
So technically, this behavior is expected.
When you're on the Resources tab and go to the ObjectsFragment, the back stack is
DashBoardFragment -> BucketsFragment -> ObjectsFragment
When you go back to the Overview tab, the back stack becomes
DashBoardFragment
And no state is saved for Fragments that aren't on the back stack. That's why when you reselect the Resources tab, it is recrated from scratch and you get back to
DashBoardFragment -> BucketsFragment
There's an existing issue for supporting multiple back stacks which aims to bring support for saving the state of each tab separately, allowing the behavior you wish. As per that issue, this requires significant changes to how Fragments work (since they are the one saving the state of Fragments) as well as integration into Navigation itself.
That issue points out a temporary workaround, demonstrated in the NavigationAdvancedSample where each tab uses its own separate navigation graph and separate NavHostFragment, thus allowing each one to keep its own state independently from one another. This requires a bit different of a setup in the MainActivity and the help of a set of NavigationExtensions to get that working.
It is expected that all of that functionality, once the multiple back stacks work is done, to be folded into the Navigation library itself.
When I navigate from one Fragment to another with the new Navigation Components, I can do it successfully with the following:
btn_walk.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.next_action))
Now, if I pop the backstack from the destination Fragment with
findNavController().popBackStack()
The Fragment that launched that Fragments recreates itself again, but the state is not being saved, I have been looking around to see how to fix it, but I don't really know if actually how to do an .add function with the navigate() from Navigation components
This gif shows the navigation from one fragment to another, when popping the stack from the destination fragment, it recreates the fragment that launched that one instead of saving the state of it.
Using the fragmenttransaction.add() method does maintain the instance of that fragment alive also if I pop the back stack.
Is there any way to do the same with Navigation Components ?
I have also readed this Navigation Architecture Fragment Reload Problem but it's unclear how it should be solved.
Fragments automatically restore their saved state when they return from the back stack: that is true with our without Navigation. Seems like the problem is with how you've coded your Fragment.
You can confirm this is a problem with your Fragment by turning on 'Don't keep activities', hitting the home button, then returning to your app and see if the Fragment restores its state perfectly. You can also test the case where you handle configuration changes (i.e., rotating the device) correctly - again, you should be able to restore your state exactly where you were. If you handle both those cases, then it'll work great in Navigation as well.
I have navigation drawer which using mostly fragments for loading views but one activity which confuse me.you can see my drawer below and assume that i have to start activity when clicking the option Profile.if i starting the activity do i need to return to home screen or i need to return to the fragment from where the activity launched ?
Problem 2 : Am replacing the selected drawer icon as you can see in the attachment below.when clicking the Profile option do i need to replace the Profile option icon as selected which is not feeling comfortable for me.?
And the genuine Problem : Drawer look like stucked or not properly ie , not smoothly closing when calling startActivity.
And one more : What about finishing navigation activity when launching new activity and starting freshly after finish the new one ?
Answer for problem 1: It is depend on you where you are writing a code i. e. click event for drawer layout. if you are using fragment then write a code in fragment and fragment is a part of activity so obviously you will going to use getActivity().startActivity(intent);
Answer for problem 2: This is not mandatory that use have to replace a icon. you can use tint functionality to change the icon color or you can use selector or use can use ripple which is default in android 5.0 and above.
Answer for problem 3: Sorry i am facing also some problem so will update on this soon.
Answer for problem 4: Why you are finishing navigation activity. Ultimately you will come back to navigation activity so don't finish navigation activity. Just start new activity. once new activity finish it will come back to navigation activity.
Hope this will help for you.