A concise Android sample app (Java) demonstrating core Activity lifecycle callbacks, navigation between activities, and basic state persistence with onSaveInstanceState().
- Android (Java) • Gradle (Groovy DSL)
- compileSdk 34, minSdk 28, targetSdk 33
- applicationId:
com.lifecycle - Launcher Activity:
com.lifecycle.MainActivity - Other Activities:
com.lifecycle.ChildActivity
- When and how Android calls lifecycle callbacks (
onCreate,onStart,onResume,onPause,onStop,onDestroy,onRestart). - How to save & restore UI state with
onSaveInstanceState/onRestoreInstanceState. - Navigating between activities and observing how transitions affect callbacks.
- .gitignore
- .gitignore
- README.md
▸ app
- .gitignore
- build.gradle
- proguard-rules.pro
- readme.md
▸ src
▸ androidTest
▸ java
▸ main
- AndroidManifest.xml
▸ java
▸ res
▸ test
▸ java
- build.gradle
▸ gradle
▸ wrapper
- gradle-wrapper.jar
- gradle-wrapper.properties
- gradle.properties
- gradlew
- gradlew.bat
- local.properties
- settings.gradle
ChildActivity— overrides: onCreateMainActivity— overrides: onCreate, onStart, onResume, onPause, onStop, onDestroy, onSaveInstanceState
app/src/main/res/layout/activity_child.xmlapp/src/main/res/layout/activity_main.xml
implementation→androidx.appcompat:appcompat:1.6.1implementation→com.google.android.material:material:1.11.0implementation→androidx.constraintlayout:constraintlayout:2.1.4implementation→androidx.activity:activity:1.8.2testImplementation→junit:junit:4.13.2androidTestImplementation→androidx.test.ext:junit:1.1.5androidTestImplementation→androidx.test.espresso:espresso-core:3.5.1
-
Prerequisites
- Android Studio (latest stable)
- JDK 17+
- Android SDK & Build-Tools for API 34
-
Open & Build
- Open this folder in Android Studio:
AndroidLifeCycles/. - Let Gradle sync and index the project.
- Open this folder in Android Studio:
-
Run
- Select an emulator or device (API 28+).
- Use the Run button to launch MainActivity (
com.lifecycle.MainActivity).
-
Try It
- Navigate to ChildActivity from the UI (if provided) or via an explicit
Intent. - Rotate the device or send the app to background/foreground to watch lifecycle logs.
- Observe
onSaveInstanceStatebeing called when rotating or process-kill occurs.
- Navigate to ChildActivity from the UI (if provided) or via an explicit
-
MainActivity
- Demonstrates the full lifecycle, overriding:
onCreate,onStart,onResume,onPause,onStop,onDestroy,onSaveInstanceState. - Use logs or on-screen indicators to visualize callback order.
- Demonstrates the full lifecycle, overriding:
-
ChildActivity
- Simple secondary screen to trigger lifecycle transitions when navigating forward/back.
Tip: Open Logcat and filter by your tag to see callback order clearly.