0% found this document useful (0 votes)
214 views4 pages

Practical No 3 Program To Show Lifecycle of An Android Project

This document describes an Android program that demonstrates the lifecycle of an Android project. It includes the XML layout file MainActivity.xml, which contains a TextView, and the Java code MainActivity.java, which contains implementations of lifecycle callback methods like onCreate(), onStart(), onStop(), etc. that display Toast messages. These lifecycle methods allow the activity to perform operations when transitions between states like running, paused and stopped occur.

Uploaded by

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

Practical No 3 Program To Show Lifecycle of An Android Project

This document describes an Android program that demonstrates the lifecycle of an Android project. It includes the XML layout file MainActivity.xml, which contains a TextView, and the Java code MainActivity.java, which contains implementations of lifecycle callback methods like onCreate(), onStart(), onStop(), etc. that display Toast messages. These lifecycle methods allow the activity to perform operations when transitions between states like running, paused and stopped occur.

Uploaded by

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

Practical no 3

Program to show lifecycle of an android project.


MainAcitvity.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:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Lifecycle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.pract3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"onCreate method is invoked",Toast.LENGTH_SHORT).show();
}

@Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"onStart method is invoked",Toast.LENGTH_SHORT).show();
}

@Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(),"onStop method is invoked",Toast.LENGTH_SHORT).show();
}

@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(),"onRestart method is invoked",Toast.LENGTH_SHORT).show();
}

@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"onDestroy method is invoked",Toast.LENGTH_SHORT).show();
}

@Override
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(),"onPause method is invoked",Toast.LENGTH_SHORT).show();
}

@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"onResume method is
invoked",Toast.LENGTH_SHORT).show();
}
}

Output:-
Theory:

Android Activity Lifecycle methods

Method Description

onCreate called when activity is first created.

onStart called when activity is becoming visible to the user.

onResume called when activity will start interacting with the user.

onPause called when activity is not visible to the user.

onStop called when activity is no longer visible to the user.

onRestart called after your activity is stopped, prior to start.

onDestroy called before the activity is destroyed.

You might also like