DialogFragment closes when navigating from it to another fragment - android

Currently in my navgraph i have dialog fragment destination ( Fragment(dialog) type). When i'm navigating to this dialog and then try to navigate to another fragment from this dialog destination, dialog is closing which in my opinion is upredictable behaviour.
Now i'm only navigating to the next fragment like this.
findNavController().popBackStack(R.id.testFragment, true)
I want to dialog not closing and only navigate to another dialog like a default fragment. How can i achieve this ?

As per this issue, this is working as intended:
There's a couple of problems here relating to how Dialogs and Fragments work:
Dialogs are separate windows that always sit above your activity's window. This means that the dialog will continue to intercept the system back button no matter what state the underlying FragmentManager is in or what FragmentTransactions you do.
Operations on the fragment container (i.e., your normal <fragment> destinations) don't affect dialog fragments. Same if you do FragmentTransactions on a nested FragmentManager.
The initial release of Navigation's <dialog> support did not take these limitations into account and would treat dialog destinations just like any other in that, from Navigation's point of view, they could be put on the back stack and treated like any other <fragment> destination.
As that's not actually the case, we've made a few changes for Navigation 2.1.0-alpha06 to ensure that Navigation's state of the world matches what you actually see on the screen and prevent crashes like that in comment#5.
The summary of this is that <dialog> destinations are now automatically popped when navigate to a non-dialog and non-activity destination, such as a destination. For reference, this was done in https://android-review.googlesource.com/996359 and https://android-review.googlesource.com/1007662
So it is expected when you navigate to a non-dialog destination that any dialog destination is popped off the back stack.

Related

I want to keep AddTransactionFragment while I navigate to other fragment with BottomNavigation (Android, Kotlin)

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.

Navigation resets when switching fragments

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.

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.

Does Navigation Components popBackStack() retains Fragment state?

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.

Android Navigation Editor - Finish Activity?

I'm trying out Android's new Navigation Editor for the first time and I'm not sure if this is a missing feature, intentional omission, or if I'm missing something. I have two fragments and I want the first fragment to be able to navigate to the second one, but I want the activity to finish if back is pressed from either fragment.
With my current setup, I can navigate from mainFragment to newFragment. If I press back from the mainFragment, the activity finishes. The only piece I can't figure out is how to finish the activity when back is pressed from newFragment. I've tried every combination of Pop Behavior settings, but haven't achieved what I'm looking for.
Just set clearTask to "true" on your action.
But your use case is going against the concept of the navigation.
https://developer.android.com/topic/libraries/architecture/navigation/navigation-principles#the_app_should_have_a_fixed_starting_destination
Apps have a fixed destination which is the screen the user sees when they launch your app from the launcher. This destination should also be the last screen the user sees when they return to the launcher after pressing the back button.
See the screenshot and look for Pop Behavior. This option can be used to finish activity.
Please note: Finish activity = pop the Activity off the stack.
Select the action from the Activity to be finished, in navigation graph.
Look for drop down for Pop To.
Select the fragment(i.e. the navHostFragment of the activity to be finished).
Check Inclusive option. (i.e. From current destination point- in ur case, it's an action - to and including this fragment - in ur case navHostFragment of Activity- in the stack will be popped off the stack. And that's what we need!).

Categories

Resources