How to prevent fragment recreating when conditionally navigating to previous fragment - android

I have two fragments A and B.
A is a form which contains EditTexts and one value need to be selected from fragment B.
When using navigation component after selecting item from B and when conditionally navigating to A , EditTexts values are gone. The fragment is recreated. How to prevent this?
My intention is to get value from B to A without recreating A

Without seeing your code its difficult to give you a straight answer, but here are some general ideas
1) Add fragment A to the backstack this will persist it when you return from Fragment B
2) Find fragment A by its tag and if it exists do not create a new Fragment A when returning to it

Related

Restore fragment's previous state on rotation and position in backstack

I have an app with a single activity showing a fragment to the user. On a click on a button, the activity shows another fragment but keeping the previous one in the backstack. I have now two problems:
On rotation (particularly in the second fragment), the data entered in the EditTexts is cleared
I want the backstack to be also restored with the text previously entered in the second fragment.
The problem is that when I save data to bundle using OnSaveInstanceState(...), I successfully get it again from OnViewCreated(...) but when I use editText.setText(str), the text is not shown.
At the moment, I think this is because the fragment is then destroyed and recreated by the parent Activity.
How can I do to make it work properly?
Thanks.
Make your fragments retainable:
setRetainInstance(true);
See the following guideline on orientation change state android orientation changes `

How to identify a fragment is called from which fragment?

It's more like a conceptual question. Actually i have three fragments let's say A,B and C all are hosted in same activity. Fragment A and B are having List. In both fragments , on click of list item i am loading Fragment C. Now how to identify whether Fragment C is loaded from Fragment A or Fragment B ? I have to make changes in Fragment C's UI when it is loaded on click of any list item in Fragment A.
You would need to detect clicks in Fragment A and B. In their OnClickListeners, you could create two different setup methods for Fragment C.
on click of list item i am loading Fragment C
Since it's you who handles the click, simply add whatever you want to make other elements recognize the source of the click. Yet, I'd still say this is wrong approach - you should not really care what fragment it was literlay but rather, assuming fragments A, B reflect different data types, what data type it was. But still - you need to add that data to your bundle (or whatever you use to pass the data to fragment C)

Remove specific fragment from backstack

I have 2 fragments(Frag A, Frag B) which are shown in multi pane in landscape with different container id (R.id.containerA,R.id.containerB) respectively.
My screen flow for 1st fragment is FragA->FragA1->FragA2
My screen flow for 2nd fragment is FragB->FragB1
I am adding each fragment to backstack. So I have around 5 fragments in backstack.
Actual order of navigating is FragA->FragB->FragA1->FragB1->FragA2
Now when I press back button, I want FragB1 to be popped out first from backstack instead of FragA2. I know fragments are maintained in a stack but how to handle this particular multi pane scenario ?
Should I use reflection like mentioned in this post ?
Android Reorder Fragment Backstack
Any other alternatives ?
If you simply want to get FragB1 instead of FragA2, you can check by obtaining the name of the fragment and then go one step back if name of the fragment is FragA2. You can get the name using the following code:
FragmentManager.BackStackEntry backEntry=getFragmentManager().getBackStackEntryAt(getActivity().getFragmentManager().getBackStackEntryCount()-1);
String str=backEntry.getName();

Proper way to save and restore fragment states on backstack?

I have looked around and found variations of this question, but all of the answers seem to be ugly hacks. Is there no easy and proper way to achieve this?
Say for instance I have Activity A, which has a FrameLayout that can hold one fragment at a time. Lets say that when the Activity is first started it loads Fragment A into the FrameLayout, which consists of a ListView. When an item is selected in Fragment A it starts up Fragment B. The way I am currently doing this is by simply hiding Fragment A and then adding Fragment B since this preserves Fragment A's state. I am of course also adding this fragment transaction to the backstack.
So now Fragment A exists on the backstack. Say now I go back to my Android home screen and go to another app. While I am doing this, the Android system decides to destroy my application because the system needs more memory. When I navigate back to my application, how am I supposed to properly restore the state of Fragment B, and Fragment A which is currently on the backstack.
I cannot use setRetainInstance() since it does not work for Fragments placed on the backstack.
Essentially what I am trying to do is restore the backstack to exactly how it was before my application was forcefully closed. So Fragment A should be on the backstack (but not showing), and Fragment B should be currently showing. If I hit the back button, it should properly pop Fragment A off the backstack.
Some notes
Since the application was forcefully closed, it's savedInstanceState != null. Same thing holds for the fragments.

how to implement a Intent.FLAG_ACTIVITY_REORDER_TO_FRONT for a fragment backstack?

I would like my fragments backstack to have a single instance of every fragment added to it. In other words, when I recall a fragment already instantiated (and added to the backstack) without popping it from the backstack (i.e. with a FragmentTransaction.replace by passing its tag name) I would like it to have it ONLY on the top of the FragmentManager's backstack. I can use FragmentTransaction.addToBackstack, but this cannot delete previous fragment references and I don't want to pop the backstack to erase the previous reference, otherwise I lose part of the history I still would like to retain.

Categories

Resources