Android Navigation Component navigate to previous fragment - android

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).

Related

Back arrow not shown when start destination in the navigation graph has a loop to itself

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?

Android navigation component & Bottom nav view - Hard back button goes back to "Home" with non-saved state

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() }

Android Navigation pop animations not working

I am currently trying to use the Android Navigation component to navigate from one fragment (A) to another (B) by animating fragment B up from the bottom of the screen (over fragment A) and subsequently animating fragment B back down when the back button is pressed. I currently have an action defined for this:
<action
android:id="#+id/action_landingFragment_to_bookingFragment"
app:destination="#id/bookingFragment"
app:enterAnim="#anim/booking_screen_in"
app:exitAnim="#anim/nothing"
app:popEnterAnim="#anim/nothing"
app:popExitAnim="#anim/booking_screen_out"
app:popUpTo="#id/landingFragment" />
However, when I navigate to fragment B from fragment A currently, I get a nice smooth slide up from the bottom of the screen, hit the back button and see fragment B disappear instantly to display fragment A again, with no animation. I currently use no custom code for the back button on fragment B (though I tried navigateUp() and popBackStack() in an OnBackPressedCallback to see if I was missing something regarding these. I also wonder if there is an issue with Z-axis of the fragments (similar to this thread) but my normal enter/exit animations work fine and I have tried a more traditional set of animations (slide left/right) in which the enter and exit animations play but the pop animations once again do not.
Can anyone suggest the correct way to do back navigation such that these pop animations should be visible?

Home icon press in Navigation component bottom navigation

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

Navigation with fragments in navigation drawer

Let me describe situtation :
I have activity that has navigation drawer that has items A, B, C, D, S(PreferencesFragment). Now as user goes into app he can choose whatever he wants. Lets say he click on B this opens new fragmentB that has list of some items. Clicking on item opens new fragmentDetails and puts transaction of fragments to back stack to enable user to go back to fragmentB with list.
Let user still be on fragmentDetails, if he now chooses to go to fragmentS from nav. drawer I would like the back stack to be empty. Is there any way how to let back stack to forget about remaining transactions ? I dont mean like popBackStack, becouse this would result into for a some small amont of time showing fragmentB.
How to achive this kind of navigation ?
lupajz,
please take some time to consider on which fragments the user will have access to the navigation drawer. Most of the time when you reach a "item details" fragment you may wan't to offer a different kind of navigation.
What you want to achieve can be done using this line:
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
Nevertheless please take a look at the following blog posts I've written covering this issues:
https://aarcoraci.wordpress.com/2017/02/13/android-tutorial-drawer-and-fragment-navigation-made-easyier/
https://aarcoraci.wordpress.com/2017/02/14/android-drawer-and-fragment-navigation-a-more-real-life-scenario/

Categories

Resources