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

Mad 17

The document describes a program to create a HelloWorld activity that uses all the activity lifecycle methods to display log messages. It includes the XML layout file with a TextView, and the Java code for the MainActivity class that implements all the lifecycle methods like onCreate(), onStart(), onResume(), etc. and uses Log.d() to output messages marking when each method is called.
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)
33 views4 pages

Mad 17

The document describes a program to create a HelloWorld activity that uses all the activity lifecycle methods to display log messages. It includes the XML layout file with a TextView, and the Java code for the MainActivity class that implements all the lifecycle methods like onCreate(), onStart(), onResume(), etc. and uses Log.d() to output messages marking when each method is called.
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

Pratical 17

Exercise: Write a program to create a HelloWorld Activity using all lifecycles


methods to display messages using Log.d.

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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!" />

</RelativeLayout>

MainActivity.java:
package com.example.pr_16;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity", "onCreate() called");
}

@Override
protected void onStart() {

super.onStart();
Log.d("MainActivity", "onStart() called");
}

@Override
protected void onResume() {

super.onResume();
Log.d("MainActivity", "onResume() called");
}

@Override
protected void onRestart() {

super.onRestart();
Log.d("MainActivity", "onRestart() called");
}

@Override
protected void onPause() {

super.onPause();
Log.d("MainActivity", "onPause() called");
}

@Override
protected void onStop() {

super.onStop();
Log.d("MainActivity", "onStop() called");
}

@Override
protected void onDestroy() {

super.onDestroy();
Log.d("MainActivity", "onDestroy() called");
}
}

Output:

Pratical Related Answers:

1. Draw the activity life cycle diagram.


2. Give the hierarchy of directory structure where you store activity file.
Hierarchy: res/layout

3. Write difference between onStop() and onDestroy() methods, also


between onPause() and onResume()methods.

onStop() method onDestroy() method

onStop() method is called when onDestroy() method is called before


activity is no longer visible to user. the activity is destroyed.
Can be involved multiple times during Called once when the activity is being
the activity life cycle. destroyed
Activity partially obscured, either by Activity being finished or system is
another activity or dialog. reclaiming resources.
Used for pausing actions like Utilized for releasing resources, un-
animations or releasing UI resources. registering receivers or saving data.

onPause() onResume()
It is called when the activity is paused It is called just before the user starts
i.e. it is mostly called when you press interacting with the application. Most
the back or home button of your of the core functionalities of the app
Android device. It is an indication that are implemented in this method.
the user is leaving the activity and
starting some other activity.
Called each time the activity is about Invoked every time the activity returns
to lose focus or become inactive. to the foreground.
Activity is still visible but may not Activity is fully visible and ready to
have user focus. interact with the user.

You might also like