Mobile Application Development Practical 17

Question 1
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"?>
<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="Activity Life Cycle"
        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.practical17;

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("Activity life cycle","onCreate called");
    }
    protected void onStart()
    {
        super.onStart();
        Log.d("Activity life cycle","onStart called");
    }
    protected void onResume()
    {
        super.onResume();
        Log.d("Activity life cycle","onResume called");
    }
    protected void onPause()
    {
        super.onPause();
        Log.d("Activity life cycle","onPause called");
    }
    protected void onStop()
    {
        super.onStop();
        Log.d("Activity life cycle","onStop called");
    }
    protected void onDestroy()
    {
        super.onDestroy();
        Log.d("Activity life cycle","onDestroy called");
    }
}
Output

Leave a Comment

Your email address will not be published. Required fields are marked *