Lab - 3a - Activity Life Cycle

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

SKR4307: Mobile Applications

Sem. I 2019/2020
Lab 3a: Android Activity Lifecycle

Introduction

Android Activity Lifecycle is controlled by seven methods of


android.app.Activity class (or appcompat.app.AppCompatActivity.
The android Activity is the subclass of ContextThemeWrapper class. The
seven lifecycle method of Activity describes how activity will behave at different states.

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.

Figure 1: Android Activity Lifecycle

1
PART I

Step 1: Create a new Android project


a. Application name: Lab3a
b. Create an Empty Activity with the the following configuration:
 Activity Name: MainActivity
 Layout Name: activity_main

Step 2: Open the activity_main.xml layout file

 The activity_main.xml layout file contain one TextView UI as follows:

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.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_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Step 3: Rewrite the MainActivity.java file


 Display the content of the activity lifecycle on the logcat window.
 Import Activity and Log class in the MainActivity class as follows:

package com.example.Lab3a;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Android Lifecycle", "onCreate() invoked");
}

@Override
protected void onStart() {
super.onStart();

2
Log.d("Android Lifecycle", "onStart() invoked");
}

@Override
protected void onResume() {
super.onResume();
Log.d("Android Lifecycle", "onResume() invoked");
}

@Override
protected void onPause() {
super.onPause();
Log.d("Android Lifecycle", "onPause() invoked");
}

@Override
protected void onStop() {
super.onStop();
Log.d("Android Lifecycle", "onStop() invoked");
}

@Override
protected void onRestart() {
super.onRestart();
Log.d("Android Lifecycle", "onRestart() invoked");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d("Android Lifecycle", "onDestroy() invoked");
}
}

Step 4: Run the app on the emulator


 Open the Logcat window

Figure 2: Lab3a

3
 See the Logcat window: onCreate(), onStart() and onResume() methods
are invoked.

 Now click on the HOME button. Then see onPause() method is invoked. Then, the
onStop() method is invoked.

 On the emulator. It is on the home. Now click on the center button to launch the app
again.

Figure 3: Home screen

4
 Click on the Lab3a icon

Figure 4: Apps screen

 See the Logcat window: onRestart(), onStart() and onResume() methods


are invoked.

 The application is started again at the emulator.

 Click on the back button. See onPause() method is invoked. After a while,
onStop() and onDestroy() methods are invoked.

PART II

Step 1: Import the Toast widget into the MainActivity file.

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

5
Step 2: Use the Toast widget to show the LifeCycle methods.
 Replace the Log class with the Toast class for every method in the MainActivity
file.

Toast.makeText(MainActivity.this,"ON CREATE",
Toast.LENGTH_SHORT).show();

Step 3: Run the Lab3a again

 Execute the same tasks as the Step 4 in Part I.

Figure 5: Toast widget

You might also like