Issue
I find that when my app goes from the background to the foreground, and the OS kills my process while it was in the background, the fragment goes through its creation life-cycle all over again. There are some scenarios where I'd like for my fragment to not start. I find that even if I request my fragment manager to remove the fragment in onCreate()
, it still goes through other life cycle methods. Any way I can get it to do a clean exit while it is still in the creation life-cycle?
Solution
The short answer to your question is NO, you cannot "short-circuit" the Fragment
life-cycle in any way. The framework won't let you.
Now I'll give the long answer, the WHY. The lives of various classes, including Fragment
s and Activity
s, are designed to follow a set of life-cycle callbacks for the reason that the GC has to manage the memory of a large variety of objects (such as View
s, LoaderManager
s and many other things) in a properly arranged sequence to ensure correct cleanup. If you try to perform a "clean exit", as you call it, then you as the programmer have to take the responsibility of cleaning up the memory, which the framework won't allow you to do.
The only time a "short-circuit" or "clean-exit" of the kind you describe happens is in the very rare case when the OS kills a foreground app (extremely rare). In that case the memory is simply reclaimed by the OS kernel, not the GC.
So you need to introspect about what you actually want to achieve as a consequence of preventing a Fragment
from completing its life-cycle. Whatever your underlying intent is, trying to accomplish that by circumventing the Fragment
life-cycle is a wrong-headed approach.
Further considerations:
I think you may be looking for the detach()
method, though its hard to tell without understanding what you really want to do. The same Fragment
can be brought back to visible state by calling attach()
.
Answered By - Yash Sampat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.