In: Computer Science
Develop an Android app to document the lifecycle of an “activity”
For each callback events (onCreate(), onStart()... ), record what callback event was triggered in the log(use Log.d); the message that is written to log should be defined in “strings.xml” and getResources.getString() should be used to retrieve the message.
Implement onSaveInstanceState and onRestoreInstanceState – to track the number of times onSaveInstanceState is being called, in onRestoreInstanceState, print the value to the log file.
Tag for “Log” statement should also be defined in strings.xml and set using getResources.getString()
To retrieve value of a string token, use
getResources.getString() ; syntax is:
String greeting = getResources().getString(R.string.hello);
R is automatically generated and string is a static class, “hello”
is the name of the string in “strings.xml”
Define a variable (global)– in onSaveInstanceState, increment it by 1 and save it to the bundle; in onRestoreInstanceState, set variable to the value stored in the bundle and print it to the log file.
Perform different actions like rotating the screen orientation, back button, home button, restore this lab app – analyze how your actions impact the activity lifecycle.
//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="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
//Strings.xml
<resources>
    <string name="app_name">LogEvents</string>
    <string name="onCreate">onCreate() is called. </string>
    <string name="onStart">onStrat() is called.</string>
    <string name="OnPause">onPause() is called.</string>
    <string name="OnRestart">onRestart() is called</string>
    <string name="OnStop">onStop() is called.</string>
    <string name="onResume">onResume() is called.</string>
    <string name="onDestroy">onDestroy() is called.</string>
    <string name="onRestart">onRestart() is called.</string>
    <string name="Tag">LOG</string>
</resources>
//=====================================
//MainActivity.java
package com.example.logevents;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
    /**
     * calls when activity is first created
     * @param savedInstanceState
     */
    int counter=0;
    String logTag = " ";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        logTag = getResources().getString(R.string.Tag);
        Log.d(logTag, getString(R.string.onCreate));
    }
    /**
     * When activity is visible to user
     */
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(logTag, getResources().getString(R.string.onStart));
    }
    //User interact with activity
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(logTag, getResources().getString(R.string.onResume));
    }
    /**
     * When activity is closed
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(logTag, getResources().getString(R.string.onDestroy));
    }
    /**
     * Restart after stopping
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(logTag, getResources().getString(R.string.onRestart));
    }
    /**
     * when activity is paused
     */
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(logTag, getResources().getString(R.string.OnPause));
    }
    /**
     * Activity is not visible
     */
    @Override
    protected void onStop() {
        super.onStop();
        Log.d(logTag, getResources().getString(R.string.OnStop));
    }
    /**
     * Implement onSaveInstanceState and
     * onRestoreInstanceState – to track
     * the number of times onSaveInstanceState
     * is being called, in onRestoreInstanceState,
     * print the value to the log file.
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        counter++;
        outState.putString("counter", String.valueOf(counter));
    }
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if(savedInstanceState!=null)
        Log.d(logTag,savedInstanceState.getString("counter"));
    }
}
//==========================================
//Android menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.logevents">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
//output

//If you need any help regarding this solution ............... please leave a comment ........... thanks