I got a problem. I have master detail view and i replace fragments by fragment manager. It works but after changing some of them i got a memory error. And fragment doesn't have any pictures inside(every replacing fragment have a quite big image).
The question is how to delete those fragment from stack that I'm not using anymore? Preventing memory errors.
how to delete those fragment from stack that I'm not using anymore?
You add to the back state from the FragmentTransaction and remove from the backstack using FragmentManager pop methods:
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(myFrag);
trans.commit();
manager.popBackStack();
Related
I have a fragment manager that has more than one fragment transaction in it. Lets say transactions inside fragment manager is like A->B->C->D->E->F->G. Is there a way that i can pop a specific transaction in back stack ex frag C. I want to pop only one transaction and keep others transactions with same queue.
popBackStack() methods are not covering my case?
Is there anyone who has faced an issue like this?
When committing transactions you need to use tags.
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.mainContainer, fragment, tag).addToBackStack(tag).commit();
Then you'll be able to remove it with this tag;
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentManager.findFragmentByTag(tag)).commit();
In my app i use backstack for my fragment and popBackStack to back previous fragment.
I have problem when i back to previous fragment by popBackStack. (FragmentA->FragmentB->FragmentA). I see that in fragmentB when popBackStack to previous fragmentA, It's called onDestroy method. I find FragmentB in getFragmentManager().getFragments() but I can found it.
However, When i dump memory heap, I find x FragmentB on heap (i open fragment B x times). It increase my memory heap and then I got error OutOFMemory when I repeat flows A->B->A until full heap.
I cannot find any solution or cause of my problem. Is there any suggestion for my problem?
1> Try to maintain the fragment in the stack (add a popup while you are adding the new fragment)
For Example:
private Stack<Fragment> fragmentStack = new Stack<Fragment>();
// code to add new fragment in stack and previous fragment in back stack
FragmentTransaction ft = fragmentManager.beginTransaction();
Fragment mFragment = new Fragment(); // you can replace this with your fragment
ft.add(R.id.container, mFragment); // you can also add fragment id so that if you can reuse that fragment if it is memory.
fragmentStack.lastElement().onPause();
ft.hide(fragmentStack.lastElement());
fragmentStack.push(resultListFragment);
ft.commit();
// code to remove visible fragment from top and resume back stack fragment
if (fragmentStack.size() == 2)// used to check whether their is any fragment in stack is yes than go for back event like onbackbutton event {
FragmentTransaction ft = fragmentManager.beginTransaction();
fragmentStack.lastElement().onPause();
ft.remove(fragmentStack.pop());
fragmentStack.lastElement().onResume();
ft.show(fragmentStack.lastElement());
ft.commit();
}
2> If the above code snippet doesn't solve your problem for OutOFMemory error, then use setRetaininstance(true) in onCreate(Bundle) system callback method of the fragment lifecycle so it will not create a new fragment if its already the in memory.
I have a Fragment with MapView in it. I add Fragment to container with following code:
Fragment fragment = new MyLocationFragment()
String tag= fragment.getClass().getName();
FragmentTransaction transaction = fm.beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right);
transaction.replace(R.id.container, fragment, tag);
transaction.addToBackStack(null);
transaction.commit();
Now when user presses back, this Fragment is supposed to be removed from FragmentManager. But if execute this code :
Fragment fragment = getSupportFragmentManager().findFragmentByTag(MyLocationFragment.class.getName());
It never returns null, even if I replace other fragments in same container. What can I do to remove Fragment from FragmentManager? Am I doing something wrong here ?
When an activity is destroyed, its FragmentManagersaves out its list of fragments. When the activity is recreated, the new FragmentManager retrieves the list and recreates the listed fragments to make everything as it was before.
[Refer to book "Android Programming The Big Nerd Ranch Guide" P150]
So your should remove fragment from fragmentmanager.
Refer to this Q&A Remove old Fragment from fragment manager
Your question maybe duplicate with thisHow to get a Fragment to remove itself, i.e. its equivalent of finish()?
I'm having some troubles with my Fragments management..
It's quite new for me and I'm pretty sure you'll find an easy way to solve my problem.
So I have a NavigationDrawer in a FragmentActivity and from this, I can navigate between two fragments.
One of those two fragments has children in a ViewPager.
I'd like to change the Parent Fragment from a ChildFragment which is in the ViewPager..
With my friend Paint, I drew my problem
http://goo.gl/OGdf1c
So as you can see, I'd like to load FragmentA from Fragment2B
Thank you !
Just perform a normal FragmentTransaction and be sure to use the FragmentManager of the Activity!
FragmentManager manager = getActivity().getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
If your Activity is a FragmentActivity or ActionBarActivity you have to remember to use the support FragmentManager, aside from that everything stays the same:
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
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.