I have integrated Navigation component in my app. I'm using it along with Bottom Navigation.
I have 3 tabs [Home, Notification, Account]. Switching the navigation is working perfectly fine.
Problem for me lies here.
From Home fragment the app navigates to many other fragments Home -> FragA -> FragB -> FragC. But when i click on the Home icon in bottom navigation, I want to clear all the Fragments and come to the initial state.
Currently Im coming to the home screen, but when i click back all previous fragments FragA -> FragB -> FragC are showing up.
How can this be achieved?
Not attaching any code as it's irrelevant
You can remove your fragments from fragmentManager:
getSupportFragmentManager().beginTransaction().remove(FragA).commit();
getSupportFragmentManager().beginTransaction().remove(FragB).commit();
getSupportFragmentManager().beginTransaction().remove(FragC).commit();
and then attach it again to fragmentManager.
if you want to save the stats of fragments use detach() instead of remove.
more details is available here
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.
In my navigation graph, I have a ProfileFragment, which is the startDestination and has a loop to itself. I want to navigate to other instances of ProfileFragment while pushing them to the backstack
While the back button works as expected, the toolbar back arrow is never shown on ProfileFragment (works on other fragments) even if there are other instances of it in the backstack.
Are there any workarounds to get the get the back arrow to show up?
Context
We're migrating to use the nav component in my company, and its going ok so far.
We have a bottom navigation view with 5 tabs, and using the NavigationUI to set it up.
We have "Home" as the start destination tab for our nav graph.
Using version 2.4.2 of the navigation-* libraries.
Problem
Each tab now has its own backstack, and its state is retained, however, when:
Having the "Home" tab opened, then opening FragmentA pushed, (Now backstack of that Tab is "Home" -> "FragmentA").
Then switching to another tab, let's call it TabX.
Then clicking on the hardware back button.
Expected
As pressing back would dismiss the current tab's stack, we get back to the "Home" tab with its previous state intact? (FragmentA pushed on top of it).
What happens
We go back to the "Home" tab with only the Home fragment, FragmentA is not showing.
And the weird part is, when clicking again (reselecting) the Home tab, it now shows the previously saved state (FragmentA on top of Home).
As this is not the best UX ever, what should be done in this case? is any of those behaviours expected?
Thanks in advance!
You can check your fragments are also the same as the navigation id.
for navigation popup, you can use
findNavController().popBackStack() or
<fragment
android:id="#+id/c"
android:name="com.example.myapplication.C"
android:label="fragment_c"
tools:layout="#layout/fragment_c">
<action
android:id="#+id/action_c_to_a"
app:destination="#id/a"
app:popUpTo="#+id/a"
app:popUpToInclusive="true"/>`
Also, make sure you override the onBackPressed() method from the
host activity code as:
override fun onBackPressed() { finish() super.onBackPressed() }
I am using Navigation Component to navigate through pages in my app.
I have a ProfileFragment and users might navigate to this fragment from different fragments(like HomeFragment, ArticleFragment, and ...).
In my ProfileFragment how should I know where to navigate the user to the previous page before they opened Profile. When the user pressed Back Button how am I supposed to detect the right action.
In my fragment's toolbar, I have an arrow (ImageButton) for navigation to the previous page too. How should I handle it in its onClickListener?
Here you can see the graph of my navigation.xml
When user clicks the back arrow in the Toolbar, you can call findNavController().popBackStack(). This will move user to the last fragment in the back stack, regardless of which one was visible before (before moving to ProfileFragment).
I am creating new android application using android architecture components.
Here the scenario is,
I have created one ( no other activities ) main activity where I have put NavHostFragment along with drawerlayout. Now I have fragment for splashscreen, where I made toolbar hidden, and everything works fine.
Now, when after 5 seconds I call other fragment using navigationcontroller, in new fragment(home fragment), instead of showing icon to open drawer, it shows back button there. And on click of back button icon, it loads splashscreen again.
Any suggestions ?
This happens because the navigation graph adds fragments to backstack and when you press the back button that pops backstack and navigates to previously fragment.
I suggest you use an activity as the splash screen for better control your stack and backstack and make it as the launcher and finish it after 5 seconds then start the main activity.
Take a look Principles of Navigation