Previous fragment state are kept in backStack - android

My app is using FragmentTransaction to navigate across screens. When i want to go back to the previous fragment using back button, the content of both current and previous fragment merge, and the result is pretty ugly.
For the record, here's my Transaction code :
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, new MyFragment());
ft.addToBackStack(null);
ft.commit();
Thanks

Related

Removing a fragment from the same container

I have 3 fragments added to stage in one and the same container. Two fragments are added with addToBackStack method:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
And final one is added without back stack:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
All three co-exist fine because as I understand if a fragment is added using a backstack, then it's not destroyed when another fragment is added.
The last one is added without a backstack, meaning that once I call replace it should be destroyed.
However, this does not happen and when I call
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, myNewFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
I can still see that there are 4 fragments total in my R.id.container, which is not that I expect.
How can I make my 3rd fragment disappear when I call replace method when adding 4th fragment?
By calling addToBackStack(), the replace transaction is saved to the
back stack so the user can reverse the transaction and bring back the
previous fragment by pressing the Back button.
So in your case you are replacing the third fragment with the fourth fragment . It means for reversing the transaction they need fragment3 also . So they will not destroy it.

How to reload fragment in ViewPager each time the page is selected?

I am using mViewPager.setOnPageChangeListener to detect when a user navigated to a specific page. Each time he navigates to this specific page, I have to reload the Fragment or return it to its original state.
I tried using replace but the app breaks on the first parameter which is the ViewPager itself.
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment fragment = new MyFragment();
fragmentTransaction.replace(R.id.viewPager, fragment);
fragmentTransaction.commit();
I tried to use the adapter itself, but I was brought to a dead end.
Where am I making a mistake? How can I reload the fragment each time a user navigate to a specific page?

Is it OK to addToBackStack and replace in a fragment transaction?

Any thoughts on the following code? In my testing I've found the replaced fragment isn't destroyed and the instance is still around when popping the back stack. Just looking to verify that this is a valid way to use fragment transactions.
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(frame, fragmentB).commit();
My reason for using replace is that it causes the replaced fragment to run it's exit animation.
You can refer to the android designer guide for fragment transaction:
http://developer.android.com/guide/components/fragments.html
Specificly the snippet below:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
So yes, what you are doing is the correct approach in replacing fragments.

Weird android fragment issue using back stack

I have following issue with Android compatibility package fragments.
There is following hierarchy of fragments:
A(login) -> B(dashboard) -> C(details)
Login fragment is added with function:
private void addFragment(Fragment f) {
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.replace(R.id.main_content, f);
ft.commit();
}
After successfull login dashboard is added same way, without adding transaction to backstack. C fragment is added like:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main_content, f, id);
ft.addToBackStack(null);
ft.commit();
So basically on detail screen I have logout button, which should bring me to login A and remove all fragments from backstack. According to android developer docs:
Whereas, if you do call addToBackStack() when removing a fragment, then the fragment is stopped and will be resumed if the user navigates back.
But it is not the issue in my case. When logout is pressed in C fragment:
getFragmentManager.popBackStackImmediate();
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.replace(R.id.main_content, new LoginFragment());
ft.commit();
onActivityCreated(), onStart() of B fragment are also called (instead of onResume written in docs), making my code crash because in this fragment Im starting some thread operation, and after adding login fragment I got IllegalStateException that fragment B is not attached to an activity (when thread operation is over it updates fragment UI) Do anyone knows how replace really works and how overcome this problem?
I guess you should call
addToBackStack for each fragment you add giving a different name to them.
Reading your code seems to me you don't do it.

Android Currentview fragment by Tag giving null every time

In my android application i am using fragments and i wnat to get the displayed fragment by tag for that i am using below code to get the fragment by tag and i am using below code to replacing the current fragment with another fragment i am now understanding what is the wrong in my code so any one guide my to correct way.
DigitalDisplayFragment myDigitalDisplayFragment = (DigitalDisplayFragment)getSupportFragmentManager().findFragmentByTag("DisplayFragment");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, displayFragment, "DisplayFragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();

Categories

Resources