0% found this document useful (0 votes)
70 views8 pages

Lab Assignment-3

The document describes creating an Android application to demonstrate the lifecycle of an activity. It includes implementing the onCreate, onStart, onResume, onPause, onStop, onRestart, and onDestroy methods. Toast notifications and LogCat are used to display when each method is called as the activity transitions between states.

Uploaded by

S.ganga priya
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)
70 views8 pages

Lab Assignment-3

The document describes creating an Android application to demonstrate the lifecycle of an activity. It includes implementing the onCreate, onStart, onResume, onPause, onStop, onRestart, and onDestroy methods. Toast notifications and LogCat are used to display when each method is called as the activity transitions between states.

Uploaded by

S.ganga priya
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
You are on page 1/ 8

ASSIGNMENT-3

1. Create an application to show the lifecycle of an activity

2. Include the following functions: -


a. onCreate
b. onStart
c. onResume
d. onPause
e. OnStop
f. OnRestart
g. OnDestroy

3. Use Toast and logcat to show when each function gets called

File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>  
2. <android.support.constraint.ConstraintLayout xmlns:android="http://
schemas.android.com/apk/res/android"  
3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
4.     xmlns:tools="http://schemas.android.com/tools"  
5.     android:layout_width="match_parent"  
6.     android:layout_height="match_parent"  
7.     tools:context="example.app.com.activitylifecycle.MainActivity">  
8.   
9.     <TextView  
10.         android:layout_width="wrap_content"  
11.         android:layout_height="wrap_content"  
12.         android:text="Hello World!"  
13.         app:layout_constraintBottom_toBottomOf="parent"  
14.         app:layout_constraintLeft_toLeftOf="parent"  
15.         app:layout_constraintRight_toRightOf="parent"  
16.         app:layout_constraintTop_toTopOf="parent" />  
17.   
18. </android.support.constraint.ConstraintLayout>  
File: MainActivity.java
1. package example.app.com.activitylifecycle;  
2.   
3. import android.app.Activity;  
4. import android.os.Bundle;  
5. import android.util.Log;  
6.   
7. public class MainActivity extends Activity {  
8.   
9.     @Override  
10.     protected void onCreate(Bundle savedInstanceState) {  
11.         super.onCreate(savedInstanceState);  
12.         setContentView(R.layout.activity_main);  
13.         Log.d("lifecycle","onCreate invoked");  
14.     }  
15.     @Override  
16.     protected void onStart() {  
17.         super.onStart();  
18.         Log.d("lifecycle","onStart invoked");  
19.     }  
20.     @Override  
21.     protected void onResume() {  
22.         super.onResume();  
23.         Log.d("lifecycle","onResume invoked");  
24.     }  
25.     @Override  
26.     protected void onPause() {  
27.         super.onPause();  
28.         Log.d("lifecycle","onPause invoked");  
29.     }  
30.     @Override  
31.     protected void onStop() {  
32.         super.onStop();  
33.         Log.d("lifecycle","onStop invoked");  
34.     }  
35.     @Override  
36.     protected void onRestart() {  
37.         super.onRestart();  
38.         Log.d("lifecycle","onRestart invoked");  
39.     }  
40.     @Override  
41.     protected void onDestroy() {  
42.         super.onDestroy();  
43.         Log.d("lifecycle","onDestroy invoked");  
44.     }  
45. }  

OUTPUT:
we will not see any output on the emulator or device. we need to open logcat.
we see on the logcat: onCreate, onStart and onResume methods are
invoked.

by clicking on the HOME Button. we will see onPause method is invoked.

After a while, we will see onStop method is invoked.


We see on the emulator. It is on the home. we click on the center button to launch the
app again.

Now click on the lifecycleactivity icon.


we see on the logcat: onRestart, onStart and onResume methods are invoked.

If we see the emulator, application is started again.


by clicking on the back button. We will see onPause methods is invoked.
After a while, we will see onStop and onDestroy methods are invoked.

You might also like