Android Navigation Architecture component avoid Fragment recreation - android

I have the following flow where within Fragment's content is a Form with various input fields.
Fragment A -> Fragment B -> Fragment C -> Fragment D ...
when the user has filled, for example, the Frag C completely, and move back to Frag B, all the Frag B data is stored and kept as intact, but when moving forward back to C, all the input data is gone.
Imagine the same scenario, the user Filled Frag A, B and he had filled half of the Frag C fields, and he chooses to go back to Frag A, when he's navigating back all the input data is intact on the previous Frags (B and A), but once he decides to move forward back to C where he was, the data from B and C are lost and replaced with new fragment every new step.
So the input data is only kept when going back (android back button), when he opens the Fragment where he's already have been there before, a new Fragment with all input as blank is created.
Is it possible to keep fragment as unique whenever the user moves back or moves forward to it on Navigation architecture component?

This isn't a way to stop fragment recreation but it addresses your problem. Create an activity scoped ViewModel to hold the form data, and then have your fragments observe this ViewModel. The ViewModel will outlive the fragments, so when they are recreated they will use the value that you previously stored in the ViewModel.
How to implement ViewModel

Related

Update viewpager fragment specific position and get back to previous while backpress

I have a view-pager with two fragment in it . based on my logic i want to replace the fragment on a specific position with another fragment but when user back-press i want to go back to previous fragment i replaced with it
for clearance
i have 4 fragment A, B, C, D initially i added A and B to view-pager, then i replaced B with c in view-pager , So that i want is to go back to fragment B (replaced by C) from C (current) while pressing back button instead of going back to another activity
you can do using assign constant variable to your fragment or assign tag at time of Fragment Call.
I manage using VariableName CurrentFragment which update value on Fragment Lunch. Then after onBackpress method of your activity i handle the fragment based on Constant Variable.
Visit below Stackoverflow link where i put solution step-wise to handle multiple fragment on backpress.
Click Here to View StackOverFlowAnswer

Cannot update fragment's data (fragment in viewpager)

I have 3 activites- A,B, and C. All these 3 activities have a viewpager. And these viewpagers have 1 fragment(this fragment is common to all viewpagers). The launcher activity is A, and pressing a button in A takes me to B, and pressing a button in B takes me to C.
A-> B -> C
Now the problem is when I update any data in C's fragment, it is not updated in A's and B's fragment. But if I update any data in A's fragment and then go to B, B's fragment will have updated data but vice versa is not happening.
Note: I' using a viewpager because I'll be adding more fragments in the future.
I assume the reason this is happening is because when C's fragment is updated, A's and B's fragment is having the old instance of the viewpager (or fragment). So, when I come back to A or B, the old data is being shown.
If this is the problem what is the best solution?
I have tried using viewpager.notifyDataSetChanged(),this works but messes up the views in the fragment.
Can anybody suggest an alternative?Thank you in advance!

Restoring fragment backstack for fragment with two possible backstack paths

I have one Activity with 3 fragments which form a workflow to collect user input.
Normally, Fragment A is the first fragment -> Launches B -> Launches C. B is supposed to launch A if the back button is pressed, and similarly C's Back button is supposed to launch B.
However, in some cases, A is supposed to launch C directly, and then C's back should launch A again.
I prefer that C should not know who launched it. I.e. I want C's "backstack" to operate without C knowing who launched it.
I tried using the usual addToBackstack approach, but I'm facing a problem when the Activity gets killed after the user lets the app go into the background while C was open.
I would like the user to return to "C" instead of starting all over from A. To achieve this I'm using the saved Instance state, and detecting which fragment was previously active, and launching it from the activity.
The problem starts when the user wants to go back from C, after the Activity was recreated after being killed. C doesn't know who launched it: A or B. How do I make the Back button work as expected for this case?
Found some answers in this excellent video by Android Developers + Adam Powell:
Fragments: Google I/O 2016
In summary, Fragments and the Fragment BackStack are considered part of the navigational state of the app, so, as long as I don't mess with the backstack when the activity is launched, the OS will also restore the FragmentBackStack, thus the BackStack will know who launched C (A or B) even if the activity gets re-created. Thus, popBackStack will move from C to A or B as required.

Android - How to avoid onResume of fragments when popping them from the backstack

We have multiple screens in our app and to switch between them we use FragmentTransactions, primarily replace, and we add fragments to the backstack when we do so. While doing so we stay within the same MainActivity.
So if we transition from Fragment A to Fragment B via some button we now have a stack that looks like
B
A
If we go to another Fragment C it goes on top of the stack like so
C
B
A
However, sometimes we transition to a fragment D, such that we want to wipe out the backstack so that users can no longer navigate back through C, B, A. We now want our backstack after navigating to D to look like this.
D
But in order to do so we need to clear the backstack using:
FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
while(supportFragmentManager.getBackStackEntryCount() > 0)
{
supportFragmentManager.popBackStackImmediate();
}
clearingBackStack = false;
But when we do so, the onResume of fragments C,B and A are called. This is not the functionality we intend and can have negative consequences such as making unnecessary server calls. We have also noticed on low end devices that we see a flash of the popped fragments as they come off the stack.
We wish to avoid this behavior, is there a way to pop the entire fragment backstack without the popped fragments being activated in anyway?

Hide all Fragments in Backstack but still being able to go back

I have a small layout in my activity that I add Fragments to based on the User navigating through the app.
Assuming the user navigates thusly:
Activity -> Fragment A -> Fragment B -> Fragment C -> Button Click
I would like to be able to to hide the Fragments and show the blank Activity again.
This is how I'm adding the Fragments to the activity:
protected void addFragment(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.secondary_fragment, fragment).addToBackStack(fragment.getTitle()).commit();
}
To clear all the Fragments, I use:
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
However, is there a way to clear the fragments in a way that if the user presses back, they would be able to go back to Fragment C (as opposed to exiting the App)?
Maybe instead of pop all the backStack, you just get the fragment view by id and setVisibility to invisible?
Try starting a new instance of your Activity with a clear stack on the button press (if I'm correct in assuming this comes after C as you described). This way the First Activity instance will still have up to Fragment C and the Second Activity instance will be whatever you like (Fragment A > Fragment D > Fragment F). And you won't need to pop/clear any back stack for any Activity.
HTHs

Categories

Resources