Q2

Question 2

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.

Browse Source Code

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");
    }
}

Output

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)

Last updated