Lifecycles in Android

Hakan Erbaş
3 min readDec 24, 2019

--

It should be known what to do when.

If android developer does not use any lifecycle callback methods while developing app, user will lose data whenever app is closed for any reason or android operating system decide to kill it because lack of memory. Activity (and even fragments) has some callback methods to catch these state changes. So, data that tend to be lost can be managed by these methods.

There are 5 different states in activity lifecycle. These are

  1. initialized ( app is invisible )
  2. created ( app is invisible )
  3. started ( app is visible but has no focus )
  4. resumed ( app is visible and have focus )
  5. destroyed ( app is invisible )

There are 7 callback methods while activity movements:

onCreate:

After activity created, onCreate method is called first. All static initializations are made in here. Also, it provides data (savedInstanceState bundle) from previous killed state. States changes as “Created” after method finishes.

onStart:

After oncreate, onStart is called. State changes to “Started” after method finishes but it has no focus. App is becoming visible.

onResume:

After onStart, onResume is called. App is on foreground and it has focus. User can have interact in this step. It is followed by onPause.

onPause:

App is visible but losing the foreground state. UI is still shown to user, so UI should be updated still. From here, activity can be resumed (foreground) or stopped (invisible to user). State is becoming paused.

onStop:

App is not visible. Now, another activity may be on top of stack. This method is used to stop animations and refreshing the UI as general. So, memory is not engaged any more. Activity can be destroyed or restarted after this method.

*onRestart

If activity becomes on top of stack again after activity became stopped, this method is called (user navigated back to it). After this method, onStart is called.

onDestroyed

Before activity is destroyed, this method is called. OS may temporarily destroy activity because lack of memory or it is going to be finished. We can check the situation by isFinishing method.

Standart activity lifecycle graphic is below.

For some reasons, it needs to directly navigate to onDestroy method. There is a method which name is finish(). Wherever we call finish(), it calls directly onDestroy() and does not call other methods.

Best exercise for using lifecycle methods is stopping an countdown timer while app is not visible.

Scenario: user started to timer and pressed the home button. Activity is not visible but timer is still working. When user bring application from stack, user sees that time passed in timer.

This may be a usage but we want that timer is stopped while it is invisible.

Activity is visible after the onStart method, while it is invisible after the onStop method. So countdown timer starter method is called in onStart method (if it is started by user. Otherwise, it should not be started). In onStop method, countdown timer stopper method should be called.

We can observe lifecycles in classes. By this way, we can manage the code when states of activity is changed.

Class should take lifecycle as parameter.

class Company(lifecycle: Lifecycle) : LifecycleObserver {
init {
lifecycle.addObserver(this)
}

Then, use annotation for changes: OnLifecycleEvent(Lifecycle.Event.ON_START)

@OnLifecycleEvent(Lifecycle.Event.ON_START) 
fun startCompany() {...}

In main activity or fragment, pass the lifecycle in as parameter:

company = Company(this.lifecycle)

Now, when onStart is called, startCompany method will be executed.

activity lifecycle graphic: https://codelabs.developers.google.com/

--

--