Fragments remain after Activity get's killed and recreated
Asked Answered
H

4

29

I have a FragmentActivity (Support Fragments) where I create Fragments by code and put them into FrameLayouts. It all works fine so far. Now if I leave the App an return everything is fine as long as the system doesn't kill my Activity (or I do that with the stop button in DDMS). If that happens nothing get's called and my Activity gets killed. onDestroy is not called.

So when I reopen my App all of the Fragments still exist and I get NullPointerExeptions because they try to do their work. The Fragments are not supposed to exist in this state of the App so that's problem for me.

I don't need them in the backStack so I don't put them there and cant call popBackStack() to get rid of them.

How can I reset my FragmentManager in onCreate() or just make sure that the Fragments get destroyed as well?

Handler answered 16/5, 2013 at 14:31 Comment(1)
have you found out the solution for this ?Verner
S
6

The fragment's lifecycle is similar to the activity's lifecycle so it can be killed and recreated by the system in order to save memory for other apps. This means that there is a mechanism for saving and restoring the fragment's state. You should use it.

If it's impossible to save the fragment's state (due to its data being dynamic and non serializable), try to implement some default behavior for non initialized state.

Solitude answered 19/2, 2014 at 17:24 Comment(0)
W
3

This actually could be normal. Instead of instantiating and destroying a lot of Fragments, Android may be keeping them around. This especially happens with ViewPagers.

Are you using a lot of getActivity() calls or calls getting the context? If so, null check the getActivity() call by doing something as simple as

Activity activity = getActivity();
if ( activity == null )
{
   Log.w("activity null!", "This will cause a crash if you access this variable!");
}

Another tip would be to watch out for AsyncTasks. If you have any of these running in the background, you would want to track if the Activity is alive or dead. I think there's lifecycle checks put into later versions of Android, but you can also keep a boolean flag on onAttach() and onDetach().

Woorali answered 19/2, 2014 at 16:33 Comment(1)
so what next? After ascertaining getaActivity() is returning null, how to recreate the activity or how to close the app?Hiedihiemal
S
2

I know it is realy late but I have the same problem and I found the answers here

support FragmentPagerAdapter holds reference to old fragments

and here

ViewPager and fragments — what's the right way to store fragment's state?

I guess that the problem is that when you create activity you create and save fragments in some list and not in fragment menager. When activity is created again it also creates a fragment but fragment manager uses the fragment that was created before. That is why your fragment returns null for getActivity() because this fragment is the one created before you put the app in background.

Saskatoon answered 2/4, 2014 at 13:9 Comment(1)
Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.Abeyta
N
0

Not sure if it is the same problem, but some time ago I had a problem with fragments in a viewpager where the fragment lifecycle methods were never called and the problem was with the fragment manager.

So, try using childFragmentManager instead of supportFragmentManager and see if it fixes your issue.

Nestle answered 16/5, 2013 at 14:49 Comment(3)
If your Activity gets killed, onDestroy gets called. And I'm quite sure that when your activity gets killed, the fragments go with it. What makes you think your Activity does get killed? You sure there's nothing that keeps references to your fragments?Lockett
The problem is that onDestroy doesn't get called for for sure. But when onCreate is called I know it's been destroyed. All my references are in the destroyed activity so no.Handler
You definitely cannot rely on onDestroy() to get called, and there's possible side-effects like managing to keep onDestroy() from finishing. It can be a messWoorali

© 2022 - 2024 — McMap. All rights reserved.