0% found this document useful (0 votes)
7 views6 pages

Android Activity Lifecycle Explained

An Android Activity represents a single screen in an application and serves as an entry point for user interaction. Activities go through various lifecycle stages, managed by callback methods such as onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(). Each method serves a specific purpose in managing the activity's state and resources during its lifecycle.

Uploaded by

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

Android Activity Lifecycle Explained

An Android Activity represents a single screen in an application and serves as an entry point for user interaction. Activities go through various lifecycle stages, managed by callback methods such as onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(). Each method serves a specific purpose in managing the activity's state and resources during its lifecycle.

Uploaded by

Dharani Mani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Android Activity

In android, Activity represents a single screen with a user interface (UI) of an


application and it will acts an entry point for users to interact with an [Link],
the android apps will contain multiple screens and each screen of our application will
be an extension of Activity class. By using activities, we can place all our android
application UI components in a single screen.
From the multiple activities in android app, one activity can be marked as a main
activity and that is the first screen to appear when we launch the application. In
android app each activity can start another activity to perform different actions

based on our requirements. Android Activity


Lifecycle
the activities in our android application will go through a different stages in their life cycle. In
android, Activity class have 7 callback methods
like onCreate(), onStart(), onPause(), onRestart(), onResume(), onStop() and onDestroy() to describe
.

onCreate()
This is the first callback method and it fires when the system creates an activity for the first time.
During the activity creation, activity entered into a Created [Link] we have an application start-up
logic that needs to perform only once during the life cycle of an activity, then we can write that logic
in onCreate() method

protected void onCreate(Bundle savedInstanceState) {


[Link](savedInstanceState);
setContentView([Link].activity_main);

onStart()
The onStart() callback method will invoke when an activity entered
into Started State by completing onCreate() method. The onStart() method will
make an activity visible to the user and this method execution will finish very
quickly.

protected void onStart()


{
[Link]()
}

onResume()
When an activity entered into Resumed state, the system invokes onResume() call
back method. In this state activity start interacting with the user that means user
can see the functionality and designing part of an application on the single screen.
Mostly the core functionality of an app is implemented in onResume() method.
public void onResume() {
[Link]();
if (mCamera == null) {
initializeCamera();
}

onPause()
Whenever the user leaves an activity or the current activity is being Paused then the
system invokes onPause() method. The onPause() method is used to pause
operations like stop playing the music when the activity is in a paused state or pass
an activity while switching from one app to another app because every time only one
app can be focused.
public void onPause() {
[Link]();
if (mCamera != null) {
[Link]();
mCamera = null;
}
}

onStop()
The system will invoke onStop() callback method when an activity no longer visible
to the user, the activity will enter into Stopped state. This happens due to current
activity entered into Resumed state or newly launched activity covers complete
screen or it’s been destroyed.

The onStop() method is useful to release all the app resources which are no longer
needed to the user.

protected void onStop()

[Link]();

onRestart()
The system will invoke onRestart() method when an activity restarting itself after
stopping it. The onRestart() method will restore the state of activity from the time
that is being stopped.

The onRestart() callback method in android activity will always be followed


by onStart() method.

onDestroy()
The system will invoke onDestroy() method before an activity is destroyed and this is
the final callback method received by the android activity.

The system will invoke this onDestory() callback method either the activity is
finishing or system destroying the activity to save space.
public void onDestroy()
{
[Link]();
}
Example of android LifeCycle

package [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Log.d("Activity Lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
[Link]();
Log.d("Activity Lifecycle","onStart invoked");
}
@Override
protected void onResume() {
[Link]();
Log.d("Activity Lifecycle","onResume invoked");
}
@Override
protected void onPause() {
[Link]();
Log.d("Activity Lifecycle","onPause invoked");
}
@Override
protected void onStop() {
[Link]();
Log.d("Activity Lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
[Link]();
Log.d("Activity Lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
[Link]();
Log.d("Activity Lifecycle","onDestroy invoked");
}
}
Output of Android Activity Lifecycle Example
Now open Android Device Monitor

Now click on Home button in Android Emulator,


After a while, the activity will enter into Stopped state and system will
invoke onStop() method

You might also like