Activities, Intents & App Lifecycle
Android SDK: Activities, Intents & App Lifecycle The Android SDK provides the APIs to build Android apps. An Activity is the entry point for user interaction — …
Android SDK: Activities, Intents & App Lifecycle
The Android SDK provides the APIs to build Android apps. An Activity is the entry point for user interaction — one screen. Apps are built from Activities, Services, BroadcastReceivers, and ContentProviders.
Activity Lifecycle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// View created — initialize UI, restore state
}
override fun onStart() {
super.onStart()
// Activity visible but not interactive
}
override fun onResume() {
super.onResume()
// Activity in foreground — start animations, sensors
}
override fun onPause() {
super.onPause()
// Partially obscured — pause animations, save draft state
// Keep it fast: next activity waits for onPause to complete
}
override fun onStop() {
super.onStop()
// Activity not visible — release heavy resources
}
override fun onDestroy() {
super.onDestroy()
// Activity finishing — final cleanup
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("key", "value") // save state before rotation/kill
}
}Intents
Intents are messages that request an action — launching an Activity, Service, or broadcasting an event. Explicit intents name the target component; implicit intents describe the action and let the system find a handler.
// Explicit intent — launch a specific activity
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra("item_id", 42)
intent.putExtra("title", "Hello")
startActivity(intent)
// Receive extras in target activity
val itemId = intent.getIntExtra("item_id", -1)
val title = intent.getStringExtra("title")
// Implicit intent — let system pick handler
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com"))
startActivity(browserIntent)
val shareIntent = Intent.createChooser(
Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Check this out!")
},
"Share via"
)
startActivity(shareIntent)
// Start activity and get result (modern API)
val launcher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
val data = result.data?.getStringExtra("result_key")
}
}
launcher.launch(intent)AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.MyApp">
<!-- Main activity -->
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Other activities -->
<activity android:name=".DetailActivity" android:exported="false" />
<!-- Service -->
<service android:name=".MyService" android:exported="false" />
<!-- BroadcastReceiver -->
<receiver android:name=".MyReceiver" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>Runtime Permissions (Android 6+)
// Request dangerous permissions at runtime
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
accessCamera()
} else {
showPermissionRationale()
}
}
fun checkAndRequestCamera() {
when {
ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED -> accessCamera()
shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) ->
showRationale() // user previously denied
else -> requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}
}
// Multiple permissions
val launcher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
val cameraGranted = permissions[Manifest.permission.CAMERA] == true
val locationGranted = permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true
}