📝
Android Programming
  • Introduction
  • Contents
  • Android
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
    • End
Powered by GitBook
On this page
  • Create an application to display various android activity lifecycle phases.
  • Code
  • Output
  1. Android

Q2

Question 2

PreviousQ1NextQ3

Last updated 4 years ago

Create an application to display various android activity lifecycle phases.

App Insights:

In the basic Empty Activity application template, wrote onStart(), onRestart(), onResume(), onPause(), onStop(), and onDestroy() methods to print the lifecycle phase to console.

Code

package com.example.q2_activity_lifecycle_phases;

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

public class MainActivity extends AppCompatActivity {
    String str="LifecycleStates";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(str,"OnCreate State");
    }
    protected void onStart(){
        super.onStart();
        Log.d(str,"OnStart State");
    }
    protected void onRestart(){
        super.onRestart();
        Log.d(str,"OnRestart State");
    }
    protected void onResume(){
        super.onResume();
        Log.d(str,"OnResume State");
    }
    protected void onPause(){
        super.onPause();
        Log.d(str,"OnPause State");
    }
    protected void onStop(){
        super.onStop();
        Log.d(str,"OnStop State");
    }
    protected void onDestroy(){
        super.onDestroy();
        Log.d(str,"OnDestroy State");
    }
}
<?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 LifeCycle Process"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Output

Download to Run and Test Locally

Q2_Activity_Lifecycle_Phases.apk
Browse Source Code
Studio Screenshot (Before Launch) (1/12)
App Screenshot (Before Launch) (2/12)
Studio Screenshot (After Install and Launch) (3/12)
App Screenshot (Before Launch) (4/12)
Console Output for Launch Activity (5/12)
Studio Screenshot (After Pressing Back Button) (6/12)
Console Output on Pressing Back Button (7/12)
Studio Screenshot (On Resuming App) (8/12)
App Screenshot (Before Launch) (9/12)
Console Output on Resuming App (10/12)
Studio Screenshot (On Closing App) (11/12)
Console Output on Closing App (12/12)