Android Activity Lifecycle: A Quick Overview
Every Activity transitions through states managed by these core methods:
onCreate(): Initializes UI and data.
onStart(): Activity becomes visible.
onResume(): User can interact (foreground).
onPause(): Activity loses focus (e.g., dialog opens).
onStop(): Activity is no longer visible.
onRestart(): Restarts after onStop().
onDestroy(): Final cleanup before termination.
XML File
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:padding="16dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="Lifecycle Events:\n" android:textStyle="bold" /> </LinearLayout>
JAVA File
package com.vipul.p6;
import android.os.Bundle;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class ActivityLifeCycle extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_life_cycle);
textView = findViewById(R.id.textView);
}
@Override
protected void onStart() {
super.onStart();
textView.setText("onStart");
}
@Override
protected void onResume() {
super.onResume();
textView.setText("onResume");
}
@Override
protected void onPause() {
super.onPause();
textView.setText("onPause");
}
@Override
protected void onStop() {
super.onStop();
textView.setText("onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
textView.setText("onDestroy");
}
}
If you have any query then comment below.
0 Comments
If you have any doubts, Please let me know