Question

I have read about android life cycle in a couple of books and official documentation, but still am unable to get my thoughts in one place and understand it completely. I am developing simple app, in which I use fragments, so this makes it even harder, as fragments have separate life cycle from applications. So I would like to ask you a couple of questions:

My application is a simple logger of various data, so basically user only needs to enter data and look at it. As I understand, cycles like onPause, onResume, onStop I shouldn't worry about? I guess my real question is which android life cycle methods are essential for every application?

Is it considered to be a very bad practice if you are (fragment vise) calling all methods and managing all views in onCreateView?

Do you know any simple and yet good guides which could help me to understand how to manage android life cycle correctly?

Was it helpful?

Solution

OnResume and onPause are very important part of the lifecycle, and you should worry about it. Whenever a user change from you App to another, o goes to the notifications, or whatever, always will be calling onPause() when it goes to another app, and onResume() when it came back. You have to understand that you Activity may be killed, (if the system don't have enough resources), in that case onCreate will be called first, but if not, then will jump the onCreate and goes to onResume(). onStop is mostly no necessary, as you should free all your resources in onPause() because after the onPause call, you don't know if your activity will be killed by the system.

Fragments includes the same Activity lifecycle callbacks plus their own callbacks.

OTHER TIPS

Mind that:

  • Having views attached to fields in Your Fragment can lead to memory leaks

  • Fragment can be detached from Activity - storing activity as variable can lead to memory leak

http://developer.android.com/guide/components/fragments.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top