3.Create an Activity that demonstrates the Life Cycle of an Activity.

 

Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 

        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:id="@+id/tv"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content"             

    android:baselineAligned="false" 

    android:text="Activity Life Cycle State" android:textSize="25sp" 

/>

</LinearLayout>

Layout Preview:

MainActivity.java:

package com.uday.android.a5a4_lc3;

import androidx.appcompat.app.AppCompatActivity; 

import android.os.Bundle;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity 

    TextView tv;

    @Override

    protected void onCreate(Bundle savedInstanceState) 

    

            super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

            tv = findViewById(R.id.tv);

            tv.setText("On Create\n");

    }

    @Override

    protected void onStart() 

    

        super.onStart(); tv.append("On Start\n");

    }

@Override

protected void onRestart()

 { 

    super.onRestart(); tv.append("On Restart\n");

}

@Override

protected void onResume() 

    super.onResume(); 

    tv.append("On Resume\n");

}

@Override

protected void onPause() 

        super.onPause(); 

        tv.append("On Pause\n");

}

@Override

protected void onStop() 

{

    super.onStop(); tv.append("On Stop\n");

}

@Override

protected void onDestroy() 

    super.onDestroy(); 

    tv.setText("On Destroy\n");

}

}

 





Output:



Post a Comment

0 Comments