Today I’m going to
talk about android activity lifecycle, the callback events you can
implement to perform each tasks in each stage of the lifecycle. And
lets see how you can handle activity states throughout activity
lifecycle. If you didn’t done this early, then you may already got
your app crashed 🙂 .

Lets start then…

About the
Activity life cycle

The activity
lifecycle is the set of states an activity can be in during its
entire lifetime. When you start an app, it initially create &
when you close the app, it destroy and the system reclaims that
activity’s resources. So be careful, because if you haven’t handle
this, then your app will be the reason why android OS get slower and
slower. When user interacts with your app, then the app will move on
to different states as you written.
Lets see this state
changes with an example
When you start
Activity 1, it comes on foreground of your device. When you start
Activity 2 on that time, Activity 2 will start and the Activity 1
will stopped. You can now see that Activity 2 is on foreground. When
you finished working on Activity 2, you can Navigate back and then
the Activity 1 will resumes. Meanwhile the Activity 2 will stops.
This is how simply App lifecycle works.

Activity states
and lifecycle callback methods

Up to now you can
see that when you open app & navigate back and doing several
things, app changes it’s lifecycle state. When app changes it’s
lifecycle states, then the Android system calls several lifecycle
callback methods at each stage. All of the callback can override in
each Activity you create. With this you can set the behaviors of your
app how it should work on each app lifecycle stages. Keep in mind
that the lifecycle states (and callbacks) are per activity, not per
app! And if you haven’t handle those things properly, user will not
be like to use your app(There will be app crashes or system slow
downs or ect.)
Following figure
shows each activity states and the callback methods, that occurs when
the activity transitions between different states.
It is not needed to
implement all the lifecycle callback methods on your activity, but it
is important to understand each one and implement those that ensure
your app behaves the expected way. If you want to create a strong
flexible application, then it is important to manage lifecycle
properly.
Lets talk about
callbacks one by one…

Activity Created
(onCreate() method)

This is the state
which your app start for the first time. When and activity is first
created, the system call onCreate() method to initialize the
activity. I guess you have seen this method already if you have
create an android app. This is the method that your have to implement
your activity initializing stuff & starting background tasks of
your activity.
Similarly, if you
have another activity in your app, which opens through an intent, the
system matches your intent request with an activity and calls
onCreate() for the new activity.

Activity started
(onStart() method)

After onCreate()
system calls the onStart() method, and the activity is in
the started state. Also the onStart() method is called if the
activity is stopped and activity returns to the foreground. This
happen in situations such as when user navigate back and returns.
This means that the onCreate() is called once and onStart()
may call in multiple times during app lifecycle.
The onStart()
method is counterpart to the onStop() method. For example,
you can connect hardware resources on onStart() method and
release hardware resources on onStop() method.

Activity resumed/
running (onResume() method)

When your activity
is in the resumed state, that means your activity is initialized,
visible on screen, and ready to use. This state is often called as
the running state, because it is the state that user interact with
your app.
onResume()
method is called just after the onStart() method is call.
Therefore onResume() method can be called multiple times, each
time the app comes back from the paused state.
The onResume()
method is the counterpart of onPaused() method. The activity
remains in this resumed state as long as the activity is in the
foreground & user is interacting with it.

Activity paused
(onPause() method)

This state can occur
in several situations.

onPause()
method is call when the system goto paused state. This is because the
system identify user may be leaving the activity, you can use
onPause() to stop animating or video playback things on
activity.

Activity stopped
(onStop() method)

Activity stopped
state occur when the activity is no longer visible on the screen at
all. Usually, the user has started another activity or returned to
the home screen. If the user retains the activity instance in the
back stack, and if the user returns to that activity, it is restarted
again. The stopped activities may be killed by android system is
resources are low.

Activity
destroyed (onDestroy() method)

When activity is
destroyed, is shut down completely and activity instance is reclaimed
by the system. This can happen in several cases.

Use onDestroy()
to fully clean up after your activity so that no component are
running after the activity is destroyed such as Threads.

Activity
restarted (onRestart() method)

This is a transient
state that only occurs if a stopped activity is started again.
onRestart() method is called in between onStop() and
onStart(). If you have something to stop and start before
starting it, then you can use onRestart() method.
OK guys, hope you
guys get to know how activity lifecycle works. Share with others so
they can learn and create good apps.

***Please note that all of the content on this post was originally published on champlnx.blogspot.com. We have migrated these posts to www.champlnx.com for your convenience and continued access.

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *