0% found this document useful (0 votes)
10 views

Lecture10 - Activity Life Cycle

The document discusses the lifecycle of Android activities, including their different states like active, paused, stopped, and destroyed. It describes the various callback methods like onCreate, onStart, onResume, onPause, onStop, onRestart, and onDestroy that are called when an activity transitions between states.

Uploaded by

Meshack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture10 - Activity Life Cycle

The document discusses the lifecycle of Android activities, including their different states like active, paused, stopped, and destroyed. It describes the various callback methods like onCreate, onStart, onResume, onPause, onStop, onRestart, and onDestroy that are called when an activity transitions between states.

Uploaded by

Meshack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Mobile programming.

Activity Life cycle


Life cycle of an Android application.

• Internally, each user interface screen is represented by an Activity


class.
• Each activity has its own life cycle
• A life cycle of an activity is the steps that an activity goes through
from start state to exit state .
• An application is one or more activities plus a Linux process to
contain them.
• In Android, an application can be “alive” even if its process has
been killed.
• This is because the activity life cycle is not tied to the process life
cycle.
• Processes are disposable containers for activities.
life cycle of an Android application.

• The following figure states of an activity during life cycle.


Activities states

• During its lifetime, each activity of an Android program can


be in one of the following states.
1. New Activity state: is a new activity that is being to loaded
to memory (i.e. allocated memory).
2. Active or running state is in the foreground of the screen
(at the top of the activity stack for the current task).
This is the activity that is the focus for the user's actions.
Activities states

3. Paused activity has lost focus but is visible to the user.


• That is, another activity lies on top of it and that new activity
either is transparent or doesn't cover the full screen.
• A paused activity is completely alive (it maintains all state
and member information and remains attached to the window
manager), but can be killed by the system in extreme low
memory situations.
Activities states

4. Stopped activity is completely obscured by another activity.


• It still retains all state and member information.
• However, it is no longer visible to the user so its window is
hidden and it will often be killed by the system when
memory is needed elsewhere.
5. Destroyed activity is killed (i.e. dropped) activity to reclaim
memory. Activity that is no longer in the activity stack
Killable States

• Killable States are states that are followed by activity


termination (destroyed state).
• They include paused state and stopped state.
Activity Lifecycle

• There are seven transition methods define the entire lifecycle of


an activity.
• The entire lifetime of an activity happens between the first call to
onCreate() through to a single final call to onDestroy().
• An activity does all its initial setup of "global" state in
onCreate(), and releases all remaining resources in onDestroy().
• Activity methods need to be overriden in the activity class so that
Android can call them at the appropriate time:
Activity life cycle methods

1. Oncreate() method
• It is the first method to be called when activity is first created (first
starts up)
• An activity does all its initial setup of "global" state in onCreate().
• It can be used to perform one-time initialization such as creating the
user interface.
• This is where normal static set up is done e.g create views, bind data to
lists, and so on.
• All activities must implement onCreate( ) method to do the initial setup
when the object is first instantiated.
• This method is passed a Bundle object containing the activity's previous
state, if that state was captured. i.e. onCreate(Bundle)
• Oncreate() is always followed by onStart()
Activity life cycle methods

2. onStart() Method
• This method is called just before the activity becomes
visible to the user or displayed to the user.
• The method is executed when the activity start running.
• It is followed by onResume() if the activity comes to the
foreground, or onStop() if it becomes hidden.
3. onResume() Method:
• This method is called when an activity result or a new intent
is delivered.
• At this point the activity is at the top of the activity stack,
with user input going to it.
• It is a good place to start animations and music.
• This method is always followed by onPause().
Activity life cycle methods
4. onPause() Method:
• This method is called when the device goes to sleep or when a new
activity is started.
• It runs when the activity is about to go into the background,
usually because another activity has been launched in front of it.
• The method is typically used to commit unsaved changes to
• persistent data, stop animations and other things that may be
• consuming CPU.
• onPause() Method is followed either by onResume() if the activity
returns back to the front, or by onStop() if it becomes invisible to
the user.
• The activity in this state is killable (destroyable) by the system
Activity life cycle methods

5. onStop() method:
• This method is called when the activity is no longer visible to the
user and it won’t be needed for a while.
• This may happen because it is being destroyed, or because
• another activity (either an existing one or a new one) has been
resumed and is covering it.
• The method is followed either by onRestart() if the activity is
coming back to interact with the user, or by onDestroy() if this
activity is going away.
• The activity in this state is killable (Destroyable) by the system.
• If memory is tight, onStop( ) may never be called (the system
may simply terminate the current process).
Activity life cycle methods

6. onRestart() Method:
• This method is called after the activity has been stopped, just
before being started again.
• If this method is called, it indicates that the activity is being
redisplayed to the user from a stopped state
• onRestart() method is always followed by onStart()
Activity life cycle methods

7. onDestroy() Method:
• This method is called just before the activity is destroyed (killed).
• It is the final call that the activity will receive.
• It could be called either because the activity is finishing or
because the system is temporarily destroying this instance of the
activity to save space.
• The activity in this state is killable (Destroyable) by the system.
• If memory is tight, onDestroy( ) may never be called (the system
may simply terminate your process).
Other activity methods

• onSaveInstanceState(Bundle):
• This method is called to allow the activity to save per-instance
state, such as a cursor position within a text field.
• Usually you won’t need to override it because the default
implementation saves the state for all your user interface
controls automatically.
• onRestoreInstanceState(Bundle):
• This is called when the activity is being reinitialized from a state
previously saved by the onSave-InstanceState( ) method.
• The default implementation restores the state of your user
interface.
Visible Lifetime

• The visible lifetime of an activity happens between a call to


onStart() until a corresponding call to onStop().
• During this time, the user can see the activity on screen,
though it may not be in the foreground and interacting with
the user.
• The onStart() and onStop() methods can be called multiple
times, as the activity alternates between being visible and
hidden to the user.
• Between these two methods, you can maintain resources that
are needed to show the activity to the user.
Foreground Lifetime

• The foreground lifetime of an activity happens between a


call to onResume() until a corresponding call to onPause().
• During this time, the activity is in front of all other activities
on screen and is interacting with the user.
• An activity can frequently transition between the resumed
and paused states
Compiling Android applications

Compiling android application is the process of converting java


source code into executable code.
It consists of the following steps:

1. Java compiler: converts java source code to java byte code (class
files).

2. Dalvik executable translator (dx) converts java byte code to dalvik


executable (.dex) file

3.Android Application package tool(aapk) bundles dalvik executable


into an archive called android package kit (.apk) file

You might also like