Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
In Android, the Activity
lifecycle is a series of states through which an activity might go during its lifetime, from the moment it's created to when it's destroyed. Understanding these states is crucial for ensuring your app behaves as expected and doesn't consume resources unnecessarily.
The main methods in the Activity lifecycle are:
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
onRestart()
Here's a simple demonstration of the Activity lifecycle:
Create a new Android project with an Empty Activity.
Replace the code in MainActivity.java
with the following:
import android.os.Bundle; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private static final String TAG = "LifecycleDemo"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "onCreate"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart"); } }
This code simply logs a message every time one of the lifecycle methods is called.
Run your app.
Monitor the Logcat
output (filter by "LifecycleDemo" to only see the relevant logs). You'll see messages as your activity goes through various states:
onCreate onStart onResume
onPause onStop
onRestart onStart onResume
onPause onStop onDestroy onCreate onStart onResume
onPause onStop onDestroy
Understanding these callbacks is crucial for handling operations appropriately. For instance, network operations might be started in onStart()
and stopped in onStop()
, while UI updates would typically be handled in onResume()
and paused or stopped in onPause()
.
Activity Lifecycle methods in Android:
These methods represent different states in the lifecycle:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialization code } @Override protected void onStart() { super.onStart(); // Code for when the activity starts } @Override protected void onResume() { super.onResume(); // Code for when the activity resumes } @Override protected void onPause() { super.onPause(); // Code for when the activity pauses } @Override protected void onStop() { super.onStop(); // Code for when the activity stops } @Override protected void onDestroy() { super.onDestroy(); // Cleanup code }
Android Activity Lifecycle diagram:
The Activity Lifecycle can be visualized with a diagram showing the transitions between different states. It's a graphical representation of the sequence of method calls during the lifecycle.
Activity Lifecycle example app in Android:
Creating a simple app to log the lifecycle methods:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("Lifecycle", "onCreate called"); } // Implement other lifecycle methods similarly }
Demo app illustrating Android Activity Lifecycle:
Create a simple app with multiple activities, and log the lifecycle methods in each activity. Observe the logs as you navigate through the app to understand the lifecycle transitions.
Handling configuration changes in Android Activity Lifecycle:
When the device configuration changes (e.g., screen rotation), the activity is recreated. To handle this, you can use the onSaveInstanceState()
and onRestoreInstanceState()
methods to save and restore the activity state.
Activity Lifecycle flowchart in Android:
A flowchart can provide a visual representation of the decisions made during the activity lifecycle. It can be complex but gives a comprehensive view of how the lifecycle methods interact.
Here's a simplified version:
+-----------+ | onCreate | +-----+-----+ | v +-----+-----+ | onStart | +-----+-----+ | v +-----+-----+ | onResume | +-----+-----+ | v +-----+-----+ | onPause | +-----+-----+ | v +-----+-----+ | onStop | +-----+-----+ | v +-----+-----+ | onDestroy| +-----------+