Skip to content Skip to sidebar Skip to footer

Unable To Destroy Activity: Java.lang.illegalargumentexception: No View Found For Id () For Fragment

I have an activity YYYY (which inflates a fragment) that extends a base activity XXXX which in turn extends ActionBarActivity. I now call finish() in onCreate() method of XXXX (at

Solution 1:

OK, I found out what the problem is. The first time around calling finish() works perfectly. The problem occurs when the application is killed by the system and is recreated. If it's just an activity that is getting recreated, calling finish() after setContentView() works fine.

But if the activity has some fragments inflated in it, then when this particular activity is killed, Android stores the states of the fragments also. But it does not save this in the FragmentManager but rather a different list. So when the activity is recreated, Android already knows what the fragments are. Hence we can't just simply destroy it before inflating the fragment. If we do that, we get the exception during onDestroy()

So the ideal place to call the finish() is not just after setContentView(), but also after the onCreateView() of the fragments. Thus I ended by calling finish() in the onResume() of the activity. Things work fine.

Though I have found out the solution, I can't seem to find how to clear the list of fragment history that Android maintains when activity is recreated. If I could clear that list, then I could call finish() in the onCreate() itself. This is something that I am still searching for.

Solution 2:

If you call finish() from onCreate() that doesnt mean that onStart and onResume are not getting called (activity is onFinishing actually). To fix your issue u should call isFinishing() at onStart and onResume and if its true then return from that point.

Solution 3:

Add finish() after setContentView()

Post a Comment for "Unable To Destroy Activity: Java.lang.illegalargumentexception: No View Found For Id () For Fragment"