How to know when a fragment comes from a popup - android

I have two fragments, A and B. Before, A and B were both top level and when I wanted to move from one to the other, I would use something like
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, BFragment.newInstance(), B_TAG)
.disallowAddToBackStack()
.commit();
Now the client wants that from B, hitting the back button, the app goes to A, so I needed to make B a child of A and therefore the transition looks like
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container, BFragment.newInstance(), B_TAG)
.addToBackStack(null)
.commit();
and from B to A
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.executePendingTransactions();
fragmentManager.popBackStack();
fragmentManager.executePendingTransactions();
The problem is that if the user changes something in B, A still has the old data.
Is there a way of knowing that I arrived at A by clicking the "back" button and poping it from the backstack so I can refresh the data?
Also, do you see a better way of handling the movement between these two fragments?

I would store the common data either:
In a singleton that handles persistent state in the application if that data can be of interest withing the whole application
In a data fragment (a fragment without any UI) that each of your UI fragments can access and change the data.
Then in each UI fragment, you can update the views when resuming the fragments.

Related

How do i get the onloaded fragment?

I wanted to know how I can get the Fragment which is onloaded on my Acticvity.
The background behind this is, that I want to change the onBackPressed method that it's switching to the right fragments. At the moment when I press "Back" the app closes, because I work alot with fragments.
Use addToBackStack on your fragment transaction. This way when you press the back key the transaction gets rolled back and thus your fragment disappears.
I got a solution for the problem:
Fragment fragment = new Fragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.Layout,Fragment);
fragmentTransaction.addToBackStack("flow1");
fragmentTransaction.commit();

Fragment Shared Element Transitions FragmentManager with add() not working

When I tried to use transitions with fragments using similar code from this article on Medium GitHub. It wouldn't work when I am using the add method() on fragment manager but it would work with replace().
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.addSharedElement(mSharedElement, "transName")
.add(D.id.content_frame, fragment, FRAG_NAME)
.addToBackStack(null)
.commit();
I need to use add() method to keep the fragment below as the user will be navigating back and forth between the first fragment which contains a list of items and the second fragment show detailed info about the item. I don't want to keep loading the fragment with the list again.
Is there a way to get shared element transitions to work when using add() on the fragment manager, than replace().

Android fragments appearing in the background

I have got a problem regarding android fragments. I have 3 fragments say A, B and C. From A I move to B and I have an if statement that checks a value from shared preferences if value exists it move to C. If i press back button on Fragment C, it navigates to Fragment A but fragment C is also visible in background. Don`t know how to fix it. I have tried almost every solution from SO questions.
Here is my code
In fragment A on button click
Fragment fragment = new MyAccount();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).addToBackStack(null)
.commit();
In fragment B
if(RegDetails.contains("MSISDN")&&RegDetails.contains("PIN")){
Fragment fragment = new ReferFriend();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment)
.commit();
}
From B it move to c and works fine. But when I click back button on C fragment A is visible but fragment C is also visible in background.
I see two options:
Add a solid background (color) to your A-fragment's root view
Use .show() and .hide()

go back button between "grand child" fragment

I have an Activity that is holding 5 fragments.
One of those fragments is holding 5 more fragments.
if i add to the fragmentManager a .addToBackStack(null).
the back button returns to the last fragment from the activity and not to the last fragment from the "father" fragment (that is holding 5 more fragments).
Any help please..
EDIT:
ACTIVITY:
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().
replace(mainContent.getId(), currentFragment)
.addToBackStack(null)
.commit();
FRAGMENT:
fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().
replace(mainContent.getId(), currentFragment)
.addToBackStack(null)
.commit();
This is probably the bug mentioned here: https://code.google.com/p/android/issues/detail?id=40323
You can workaround it by handling the 'back' manually. Refer to this thread for a lot of workarounds: Android 4.2: back stack behaviour with nested fragments
I think the issue might be with "the fragmentManager" here. There is more than one FragmentManager.
There is the FragmentManager for the Activity – Activity.getFragmentManager().
And there is the FragmentManager for child Fragments within a Fragment – Fragment.getChildFragmentManager() or Fragment.getFragmentManager().
Let's say you have an activity, a parentFragment and a childFragment. So, instead of activity.getFragmentManager(), I think in your case, you might want either childFragment.getFragmentManager() or parentFragment.getChildFragmentManager().
Note that if you are using the Support package, the names may be different.

Android - How to resume fragment from back stack if it exist?

Hello I am working with sliding menu with fragment.
Indivisual fragment works properly. But suppose user navigates From fragment A->B , now 'B' works perfect and now if user goes from B->A than fragment 'A' is called from onAttach() .
I want such a condition if any fragment is opened , than reopening it should not load whole fragment , it should be resumed just like we handle activity with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT.
Here is my code...
FragmentManager fm = MainActivity.this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Layout1 fragment = new Layout1();
ft.add(R.id.activity_main_content_fragment, fragment,Layout1.class.getName());
ft.addToBackStack(Layout1.class.getName());
ft.commit();
Answer updated:
Reading the documentation, there is a way to pop the back stack based on either the transaction name or the id provided by commit. Using the name may be easier since it shouldn't require keeping track of a number that may change and reinforces the "unique back stack entry" logic.
Since you want only one back stack entry per Fragment, make the back state name the Fragment's class name (via getClass().getName()). Then when replacing a Fragment, use the popBackStackImmediate() method. If it returns true, it means there is an instance of the Fragment in the back stack. If not, actually execute the Fragment replacement logic.
private void replaceFragment (Fragment fragment){
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}
If you return to a fragment from the back stack it does not re-create the fragment but re-uses the same instance and starts with onCreateView() in the fragment lifecycle, see Fragment lifecycle.
So if you want to store state you should use instance variables and not rely on onSaveInstanceState()
Check this link. it will help How to resume Fragment from BackStack if exists

Categories

Resources