Navigation with fragments in navigation drawer - android

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/

Related

Add a Fragment to Backstack of Navigation Graph

I am using the UI Navigation Graph. I have fragment A and B. My application starts on Fragment B to show some information. Then the user can press back and end up in A. Then from A the user usually picks something and ends up in B.
Normally I can pop B from the stack and show A. However, when the app just launches there is no A in the back stack. I guess I could add A to the backstack manually but I wonder if the Navigation UI Graph has something that lets me do this.
I cannot change the order of my application fragments. That means that I still need to show B first.
As per the Principles of Navigation, the start destination of your app should always be the last screen the user sees before they exit your app. That means that your Fragment A should be the start destination of your graph.
As per the Conditional Navigation documentation, if you have important information that the user should see (such as a first time user experience), you should have your Fragment A navigate to your Fragment B. This ensures that when you're on Fragment B, Fragment A is on the back stack already.

Creating back stack with Android Navigation Component

I am having an issue creating a back stack with the new android navigation component. I have a main graph with a splash fragment, a loginregister root fragment that has it's own graph, an onboard step 1 fragment with corresponding step 1 graph, step2 fragment with step 2 graph, and a home fragment. When the user logs in, they can go to either step1, step2, or the home. If they go to step2, I want them to be able to hit back and end up at step1, and even better, the last fragment of the step1 graph. I've tried setting global actions and setting popTo, but I often get an error saying that it's ignoring the popTo because step1 isn't part of the back stack. Having a button on the screen to go back so I can call navigate with global action works, but when using the hard back button it will go from step2 to login/register.
Even just a simple case of one graph, fragments A, B, C, D. The user can enter the app at any fragment. If I just try to navigate to fragment C with the BtoC action, then there is an error of this action is unknown to this controller. Currently I have to essentially make a couple navigate calls, one after another.
In on of the navigation training docs it says "And in both cases, you should make the Back button more predictable by inserting into the task's back stack the complete upward navigation path to the app's topmost screen. " But I can't find anywhere that really shows how to do this with the NavController and single activity apps.
Any insight would be appreciated.

Instagram type backstack navigation Android

How to implement navigation like instagram where every tab of theirs has it's own back stack?
I have a TabLayout with 5 tabs and I inflate many different fragments in each tab individually depending on user action but it all just gets added to the backStack in general, not specifically to each tab.
For example in my:
If I am in the Settings Tab-->Inflate User Frag-->Inflate Profile Pic Frag
Then I go to the Feed Tab-->Open Post Frag
Then I go back to the Settings Tab, the Profile Pic frag will still be open which is good but if I click the back button I would like it to remove the Profile Pic frag and take me back to the User Frag but instead it will remove the Open Post Frag
I've played around with FragmentTransactions and the backstack but can't seem to get it right, often the problem is too many fragments get removed when I dont want them to.
Any help will be great!

How to know if all fragments were detached?

On my app I am using a Main Activity, which has a Navigation Drawer, and as the user go to an options from the drawer, it will change the fragment beging displayed accordingly to the option selected.
If the user hit's "Back button" several times, it will go back to a point in which it will reach my Main Activity, which is a blank and empty layout.
When I reach this point (my main activit, empty), I would like to exit the app, or, open the Navigation Drawer.
The problem is, I don't know any event that shows me that I am back to the Main Activity. I checked the onResume, but it's never called, which makes sense, since the main activity has never been stopped.
I thought perhaps there would be an event from the fragment manager that would be called on the Main Activity when a fragment was detached, and from there I could check if there was no fragment at all attached?
When you push your first fragment, add a tag to it. Something like
transaction.replace( R.id.rootContainer, firstFragment, "rootFragment");
Whenever user presses back button, you can get your rootFragment from FragmentManager
FirstFragment myFragment = (FirstFragment) getSupportFragmentManager().findFragmentByTag("rootFragment");
And check if this fragment is visible or not, by myFragment.isVisible(), if not, then keep popping the stack, if it is visible, it means user is on the first fragment. Now you can either quit the app, or show your menu drawer.
Good night good sir. Thank you for your tip.
I used a different approach, based on your repply, that gave me quite a few insights.
On main activity, I Overrided the onKeyDown and check if it was pressed the Back Button. If yes, then I check the size of my Back Stack.
Uppon that, I decide if I want to finish my application.
Also, thanks for the tip of "labeling" the fragments on the back stack, didn't know I could do that.

What is the proper UX for pressing back with navigation drawer fragments?

Suppose I have 5 drawer items in Activity B. If I select 2, then 3, then 1, What's the proper UX when I press back 1 time? Go back to fragment 3, or go back to Activity A? I see SO posts where people add to the fragment backstack, so it looks like the proper UX is going back to fragment 3. But what if I kept clicking different nav drawer items? It would take a long time for me to return to Activity A.
Try building out the solutions and testing the solutions on a friend. I would also try a solution where a user has a "home button" to return to Activity A. The back button could be set to returning to the last fragment.
If the user is losing any information while navigating back to Activity A and skipping the last fragments then I would consider that poor UX.
I see the overall trend is not adding the fragments to the backstack but going to the previous activity or exiting the app. The responses here have been helpful; thank you.

Categories

Resources