You are on page 1of 5

EXPERIMENT NO.: 9 Date:22.08.

23
Aim of the Experiment: Create an activity and implement activity life
cycle’s methods.

Introduction:
The foundation of designing compelling user experiences in the context of Android app
development is the creation and management of activities. A single screen with a user interface
that a user can interact with while navigating your programme is represented by an activity. We
will learn more about this subject and gain understanding of important methods like onCreate,
onStart, onResume, onPause, onStop, and onDestroy.

Code :
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Mainactivity.java :
package com.example.activity_lifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

public class MainActivity extends AppCompatActivity {

public static final String My_tag="Hello Sarthak";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

@Override

protected void onStart(){

super.onStart();

Log.i(My_tag,"onStart");

@Override

protected void onResume(){

super.onResume();

Log.i(My_tag,"onResume");

@Override

protected void onPause(){

super.onPause();

Log.i(My_tag,"onPause");

@Override

protected void onStop(){

super.onStop();

Log.i(My_tag,"onStop");

@Override

protected void onRestart(){


super.onRestart();

Log.i(My_tag,"onRestart");

@Override

protected void onDestroy(){

super.onDestroy();

Log.i(My_tag,"onDestroy");

}}

Compilation Error :

In this program, I Faced a small syntax error while writing the code .By mistake, I typed one logcat
method in a wrong way . By correcting the method the error completely solved.

Final Output:
Active/Running state:
In this state one activity is created by using onCreate() method. Then the activity is visible on the user’s
display after using onStart() and onResume() methods. The related snapshots are attached below.
Paused state:
In this state onPause() method is used to pause the activity.

Stopped state:
In this state onStop() method is used to pause the activity.

After stopping the activity we can again restart the activity using onRestart() method.

Destroyed state: In this state the activity is destroyed by using onDestroy() method.

Conclusion:
In conclusion, an essential skill for developing Android apps with Android Studio is being able to
create and implement an activity's lifecycle methods. A smooth user experience is made possible
by these lifecycle methods, which include onCreate, onStart, onResume, onPause, onStop, and
onDestroy.

Faculty Sign: Name:


Registration No:
Batch:

You might also like