This project demonstrates the Activity Life Cycle in Android, showcasing how activities transition through different states during their lifecycle. It includes two activities (MainActivity
and SecondActivity
) to illustrate activity navigation and how lifecycle methods are triggered in response to user actions and system events.
Understanding the Activity Life Cycle is crucial for Android app development. Activities are the core building blocks of Android apps, and their lifecycle methods allow developers to manage resources efficiently, handle user interactions, and ensure a smooth app experience.
This project includes:
- Lifecycle Logging: Each lifecycle method is logged to Logcat to observe how the lifecycle transitions.
- Activity Navigation: Demonstrates starting a second activity from the main activity.
- Hands-On Interaction: Encourages students to test lifecycle methods by rotating the screen, pressing the home button, or navigating between activities.
-
Activity Life Cycle:
onCreate
: Initialize the activity.onStart
: Make the activity visible.onResume
: Start interacting with the user.onPause
: Pause user interaction (e.g., another activity partially obscures it).onStop
: Stop visibility (e.g., navigating to another activity).onRestart
: Restart after being stopped.onDestroy
: Clean up resources when the activity is destroyed.
-
Activity Navigation:
- Using
Intent
to start a new activity.
- Using
-
Testing Lifecycle Transitions:
- Rotate the device to trigger
onPause
,onStop
, andonDestroy
. - Press the home button to see
onPause
andonStop
. - Reopen the app to observe
onRestart
.
- Rotate the device to trigger
|-- app/
|-- src/
|-- main/
|-- java/com/idz/androidactivityfundamentals/
|-- MainActivity.kt # Main activity with full lifecycle methods
|-- SecondActivity.kt # A secondary activity to demonstrate navigation
|-- res/
|-- layout/
|-- activity_main.xml # Layout for MainActivity
|-- activity_second.xml # Layout for SecondActivity
|-- values/
|-- strings.xml # App string resources
|-- AndroidManifest.xml # App manifest with both activities registered
-
Clone this repository to your local machine:
git clone https://github.com/your-username/activity-lifecycle-example.git
-
Open the project in Android Studio.
-
Sync Gradle by clicking "Sync Now" when prompted.
-
Run the project on an emulator or physical device.
-
Logging Lifecycle Events:
- Open Logcat in Android Studio.
- Filter logs by
ActivityLifecycle
to view lifecycle events for both activities.
-
Navigate Between Activities:
- Launch the app.
- Click the "Go to Second Activity" button in
MainActivity
to startSecondActivity
.
-
Trigger Lifecycle Events:
- Rotate the device to trigger configuration changes.
- Press the home button and reopen the app to observe
onPause
,onStop
, andonRestart
. - Close the app to observe
onDestroy
.
-
Understand Logs:
- Check Logcat to see the order in which lifecycle methods are called.
- Understand how Activity Life Cycle works in Android.
- Learn how to navigate between activities using
Intent
. - Observe and analyze lifecycle transitions using Logcat.
- Apply lifecycle methods to manage resources efficiently.
Contributions are welcome! If you have suggestions for improving this project or extending it with new features, feel free to submit a pull request.
This project is licensed under the MIT License. You can find the full license text here.