skip to content

Android SDK

The Android framework beneath any UI toolkit: app components and their lifecycles, intents, the manifest and permissions, resources, the app build and adb. Interviewers treat it as the base layer.

on this pageshow

explore

questions

page 1 of 2

In an Android Activity, which lifecycle callbacks run, and in what order, when the user rotates the device, presses Home, and presses Back?

level: juniorimportance: must knowfreq 85%

answer

  1. six callbacks, two directions
  2. rotation means a new instance
  3. Home behaves like full cover
  4. root launcher activity since Android 12

basics

~20 s

Rotation destroys and recreates an Activity: onPause, onStop, onDestroy, then onCreate, onStart, onResume on a new instance. Home runs onPause and onStop and keeps it. Back adds onDestroy, except on a root launcher activity on Android 12+.

solid answer

~40 s

By default rotation is a configuration change, so the running instance gets `onPause`, `onStop` and `onDestroy`, and a **new** instance gets `onCreate` (with the saved `Bundle`), `onStart` and `onResume`; on API 28 and higher `onSaveInstanceState` runs after `onStop`. Pressing Home hides the activity completely, so it gets only `onPause` and `onStop`, and coming back runs `onRestart`, `onStart` and `onResume` on the same instance. Back on an ordinary activity runs `onPause`, `onStop` and `onDestroy`, and that instance is gone for good with no saved state. On Android 12 and later, Back on the root launcher activity moves the task to the background instead, like Home. Android 17 (API 37) stopped recreating activities for keyboard, navigation, touchscreen, color-mode and desk-mode changes, but rotation still recreates them.

go deeper

for a junior

Recite the six callbacks in order and say which run on rotation, Home and Back. Knowing that rotation produces a brand-new instance is the point most screeners check.

for a middle

Explain why fields vanish on rotation, where onSaveInstanceState sits relative to onStop since API 28, and the overlapping order when one activity starts another.

for a senior

Show you place work by lifecycle pairs, never trust onDestroy for persistence, and know the Android 12 root-activity Back change and Android 17's narrower restart list.

for a principal

Frame lifecycle handling as a team rule: lifecycle-aware components instead of logic in callbacks, and recreation and process death tested on every screen rather than assumed rare.

## The six callbacks and the states they mark An Android `Activity` moves through a small set of states, and the system calls one callback as it enters each one. The **core six** are: - `onCreate(savedInstanceState)` - the activity is **Created**. Do one-time setup here. The `Bundle` is `null` the first time and carries the saved instance state when the activity is being recreated. - `onStart()` - the activity is **Started** and becomes visible. - `onResume()` - the activity is **Resumed**: in the foreground and interactive. - `onPause()` - the first sign that the user is leaving. The activity is no longer in the foreground but may still be visible, for example in multi-window mode. - `onStop()` - the activity is no longer visible. - `onDestroy()` - the activity is finishing, or the system is destroying it temporarily for a configuration change. A seventh, `onRestart()`, runs when a **stopped** instance comes back, just before `onStart()`. The key interview point is that the callbacks come in mirrored pairs: what you acquire in `onStart` you release in `onStop`, and what you acquire in `onResume` you release in `onPause`. ## Rotation creates a new instance Rotation is a **configuration change**, and the platform's default answer to a configuration change is **activity recreation**. The sequence is: 1. The current instance gets `onPause`, then `onStop`. 2. On API 28 and higher the system then calls `onSaveInstanceState`; before API 28 that call came before `onStop`. 3. The current instance gets `onDestroy`. Calling `isFinishing()` here returns `false`, because the activity is coming back. 4. The system immediately creates a **new** instance in the new configuration and calls `onCreate` with the saved `Bundle`, then `onStart` and `onResume`. Because the old object is thrown away, any state kept only in its fields is lost. That is why rotation is the cheapest way to expose a state-handling bug. Android 17 (API level 37) narrowed which changes restart an activity by default. The system no longer recreates the activity for `CONFIG_KEYBOARD`, `CONFIG_KEYBOARD_HIDDEN`, `CONFIG_NAVIGATION`, `CONFIG_TOUCHSCREEN`, `CONFIG_COLOR_MODE`, or a `CONFIG_UI_MODE` change into or out of desk mode. Those arrive in `onConfigurationChanged()` instead, and an app that needs the old restart opts back in with the `android:recreateOnConfigChanges` manifest attribute. Orientation and screen size are not on that list, so rotation still recreates the activity unless it declares those changes in `android:configChanges`. ## Home, Recents and covering activities - **Home or Overview**: the system treats the activity as completely covered. It gets `onPause` and `onStop`, the object stays in memory, and its task goes to the background. Returning runs `onRestart`, `onStart` and `onResume` on the **same** instance. - **A dialog-style activity that only partly covers it**: the covered activity gets `onPause` only, and `onResume` when it regains focus. - **Activity A starts Activity B** in the same app: A's `onPause` runs, then B's `onCreate`, `onStart` and `onResume`, and only then A's `onStop` if A is no longer visible. The two transitions overlap, so B must not assume A has already saved in `onStop`. ## Back For an ordinary activity, Back runs `onPause`, `onStop` and `onDestroy`; the activity is popped from the back stack and `isFinishing()` is `true`. The user dismissed it, so the system keeps **no** instance state for it. The exception is the **root launcher activity**, the one whose intent filter has `ACTION_MAIN` and `CATEGORY_LAUNCHER`. On Android 11 and lower Back finishes it. On **Android 12 and higher** the system moves the activity and its task to the background instead, exactly as Home does, so it gets `onPause` and `onStop` but not `onDestroy`, and a later launch is a warm start. Custom back handling should use the AndroidX Activity back APIs rather than overriding `onBackPressed()`, so this system default still applies. ## Summary | Event | Callbacks on the current instance | What happens next | |---|---|---| | Rotation | `onPause`, `onStop`, `onDestroy` | a new instance runs `onCreate`, `onStart`, `onResume` | | Home or Recents | `onPause`, `onStop` | returning runs `onRestart`, `onStart`, `onResume` | | Back, ordinary activity | `onPause`, `onStop`, `onDestroy` | the previous activity resumes | | Back, root launcher activity, Android 12+ | `onPause`, `onStop` | the task waits in the background | | Partly covering activity | `onPause` | `onResume` when it closes | ## Where work belongs - Keep `onPause` short: it is not the place for database writes or network calls. - Do heavier saving in `onStop`, because a stopped activity's process is a likely target when the system needs memory. - Never rely on `onDestroy` for saving: when the system kills a background process, there is **no guarantee** `onDestroy` runs at all.

  • How can onDestroy tell a rotation apart from the user leaving the activity for good?
    Check `isFinishing()`. It returns `true` when the activity is finishing because the user pressed Back or code called `finish()`, and `false` when the system is destroying it to recreate it for a configuration change such as rotation. Better still, keep data that must outlive rotation outside the activity so `onDestroy` does not need to decide.
  • In a photo editor, where should the camera preview start and stop, and where should the draft be saved?
    Start the preview on resume and release it on pause if it should run only while the editor has focus, or pair start and stop instead if it should keep running while visible in multi-window. Do not save the draft in `onPause`, which is brief and may not finish disk or network work; save it in `onStop` or, better, when the user makes the edit.
  • What did Android 17 change about configuration changes, and how does an app get the old restart back?
    From Android 17 (API 37) the system no longer restarts an activity by default for keyboard, keyboard-hidden, navigation, touchscreen and color-mode changes, or for entering or leaving desk UI mode. The activity stays alive and receives `onConfigurationChanged()`. An app that reloads resources by restarting lists those changes in the new `android:recreateOnConfigChanges` manifest attribute.

  • Rotation only calls onPause and onResume on the same instance.
  • onDestroy always runs before the system kills a background process.
  • Pressing Home destroys the activity, so onDestroy is where to save.
  • Back always finishes the activity, even the root launcher one on Android 12+.
  • onPause is the right place for database writes and network calls.
  • When A starts B, A is fully stopped before B is created.

Assumes Android 17 (API 37): it stopped recreating activities for keyboard, navigation, touchscreen, color-mode and desk-mode changes, while rotation still recreates; Back on a root launcher activity backgrounds the task since Android 12.

open as a page

With adb, how do you get logs off a misbehaving Android device and narrow logcat to your own tags and priorities?

level: juniorimportance: must knowfreq 68%

basics

~20 s

List devices with adb devices, target one with -s, then run adb logcat with tag:priority filters ending in *:S so only your tags print; read the crash buffer for stack traces and take adb bugreport for a full capture.

open as a page

In an Android app module's build file, what do compileSdk, minSdk and targetSdk each control, and how do they relate?

level: juniorimportance: must knowfreq 72%

basics

~10 s

compileSdk sets which Android APIs your code compiles against, minSdk sets the oldest Android version that can install the app, and targetSdk declares which version's runtime behaviour the app was tested for.

open as a page

On Android, what is the difference between an explicit and an implicit intent, and how does the system resolve an implicit one?

level: juniorimportance: must knowfreq 75%

basics

~20 s

An explicit intent names the component to start, so Android delivers it there directly; an implicit intent describes an action, data and categories, and Android matches it against installed apps' intent filters, letting the user pick when several match.

open as a page

In the Android Keystore, where should the key that encrypts a session token live now that EncryptedSharedPreferences is deprecated, and what does the app hold?

level: juniorimportance: must knowfreq 72%

basics

~20 s

Generate the key inside the AndroidKeyStore provider, which security-crypto 1.1.0 points to after deprecating EncryptedSharedPreferences. The app holds only a SecretKey handle: the Keystore performs each encryption outside the app's process and never exports the key material.

open as a page

In Android's AndroidManifest.xml, what do the normal, signature and dangerous protection levels mean for how a declared permission is granted?

level: juniorimportance: must knowfreq 74%

basics

~20 s

Every permission an Android app uses is declared with <uses-permission>; its protection level decides the grant. Normal permissions are granted at install, signature permissions only to apps signed with the declaring certificate, and dangerous ones must also be requested at runtime.

open as a page

With NotificationCompat, how do you build and post a 'your washer is done' notification on Android 8.0 and later, channel included?

level: juniorimportance: must knowfreq 80%

basics

~10 s

Register a NotificationChannel with an importance once at startup, build the notification with NotificationCompat.Builder(context, channelId) and at least a small icon, title, text and tap intent, then post it with NotificationManagerCompat.notify(id, notification).

open as a page

On Android, what do dp, sp and px measure, and which unit belongs on a view's margin versus its text size?

level: juniorimportance: must knowfreq 78%

basics

~20 s

px is a physical screen pixel; dp is a density-independent pixel equal to one pixel at 160 dpi; sp is a dp that also follows the user's font-size setting. Use dp for margins and sizes, sp for text.

open as a page

On Android, how does a started Service differ from a bound one, and how does an activity reach its app's service through a local Binder?

level: juniorimportance: must knowfreq 70%

basics

~20 s

A started Service runs from startService() until stopSelf() or stopService(); a bound Service lives while clients hold a bindService() connection. In one process, onBind() returns a Binder subclass whose getService() method hands the client the service instance.

open as a page

In an Android photo editor, what survives a rotation versus a system-initiated process kill, and what does onSaveInstanceState bring back?

level: middleimportance: must knowfreq 80%

basics

~20 s

Both rotation and process death recreate the activity and its fragments as new objects. The saved instance state Bundle, views' own state and the fragment back stack come back after either; objects held outside the activity survive rotation but not process death.

open as a page

With Android Gradle plugin 9.4, how do you turn on R8 for a release build, and what does a keep rule tell it?

level: middleimportance: must knowfreq 64%

basics

~20 s

Since AGP 9.3, set optimization { enable = true } in the release build type (the older isMinifyEnabled and isShrinkResources still work); R8 then removes, rewrites and renames code and resources, and keep rules name what it must preserve.

open as a page

On Android, how does a manifest-declared broadcast receiver differ from a context-registered one, and what do RECEIVER_EXPORTED and RECEIVER_NOT_EXPORTED decide?

level: middleimportance: must knowfreq 62%

basics

~20 s

A manifest-declared receiver is registered at install and can start your app, but from API 26 most implicit broadcasts skip it; a context-registered receiver lives as long as its Context. RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED says whether other apps may reach it.

open as a page

On Android, what is a PendingIntent, and when do you create one with FLAG_IMMUTABLE rather than FLAG_MUTABLE?

level: middleimportance: must knowfreq 60%

basics

~20 s

A PendingIntent is a system-held token that lets another app or the system fire your intent later as if your app sent it. Create it with FLAG_IMMUTABLE by default; use FLAG_MUTABLE only when the holder must fill in fields, as for inline replies.

open as a page

With KeyGenParameterSpec in the Android Keystore, how do you generate an AES-GCM key for a session token, and how must encryption handle the IV?

level: middleimportance: must knowfreq 62%

basics

~10 s

Build the spec with PURPOSE_ENCRYPT or PURPOSE_DECRYPT, BLOCK_MODE_GCM and ENCRYPTION_PADDING_NONE, generate it with KeyGenerator in AndroidKeyStore, let Cipher pick a random IV, store cipher.iv beside the ciphertext, and decrypt with GCMParameterSpec.

open as a page

On Android, how do you request the camera permission with the RequestPermission contract, including the rationale and the denial paths?

level: middleimportance: must knowfreq 82%

basics

~10 s

Declare CAMERA, check it with ContextCompat.checkSelfPermission, show your own explanation when shouldShowRequestPermissionRationale returns true, then launch a RequestPermission launcher and handle its boolean. A denial degrades the feature; denying twice stops the dialog appearing.

open as a page

On Android 13 and later, how does an app get the POST_NOTIFICATIONS permission, and how does its target SDK change when the dialog appears?

level: middleimportance: must knowfreq 70%

basics

~20 s

POST_NOTIFICATIONS is a runtime permission from Android 13 (API 33): declare it, then request it. Apps targeting 33+ choose when the dialog appears; for targets of 32 or lower the system shows it once a channel exists and an activity starts.

open as a page

How does Android choose among res/ directories such as layout-sw600dp, values-night and values-ar when a news app starts on a device?

level: middleimportance: must knowfreq 66%

basics

~20 s

Android resolves each resource separately: it discards directories that contradict the device configuration, then walks the qualifier precedence table, keeping only directories with the highest-precedence qualifier any candidate uses, until one remains. Density picks the closest bucket.

open as a page

On Android 14, what must a dash-cam app declare and call so its recording Service runs as a camera foreground service?

level: middleimportance: must knowfreq 72%

basics

~10 s

Declare the service with android:foregroundServiceType="camera", request FOREGROUND_SERVICE, FOREGROUND_SERVICE_CAMERA and the runtime CAMERA permission, start it with startForegroundService(), and call startForeground() with a notification and FOREGROUND_SERVICE_TYPE_CAMERA within seconds.

open as a page

In an Android Service, what do START_STICKY, START_NOT_STICKY and START_REDELIVER_INTENT returned from onStartCommand change when the process is killed?

level: middleimportance: must knowfreq 60%

basics

~20 s

If the process dies after onStartCommand returns, START_STICKY recreates the service with a null Intent, START_NOT_STICKY leaves it stopped until the next explicit start, and START_REDELIVER_INTENT recreates it and redelivers the last Intent until stopSelf(startId).

open as a page

With BiometricPrompt and a CryptoObject, how does an Android time-clock app decrypt its Keystore-protected session token, and what breaks after a new fingerprint is enrolled?

level: seniorimportance: must knowfreq 60%

basics

~20 s

Init an AES-GCM Cipher in DECRYPT_MODE with the per-use key, wrap it in BiometricPrompt.CryptoObject, call authenticate(promptInfo, cryptoObject) and run doFinal in onAuthenticationSucceeded. After a new enrolment, Cipher.init throws KeyPermanentlyInvalidatedException: delete the key and sign in again.

open as a page

On Android 12+, why does a dash-cam app's camera foreground service, started from a receiver when the car's Bluetooth connects, throw ForegroundServiceStartNotAllowedException, and what do you change?

level: seniorimportance: must knowfreq 62%

basics

~20 s

Apps targeting Android 12 (API 31) cannot start foreground services from the background except in listed cases, and a Bluetooth broadcast is not one. Post a notification whose Start recording action starts the service, or start it from the visible app.

open as a page

What does Android view binding generate from an XML layout, and what does it fix compared with findViewById?

level: juniorimportance: should knowfreq 58%

basics

~20 s

View binding generates one class per layout, such as ResultProfileBinding, with a typed field for every view that has an id plus a root reference. Wrong ids or types fail at compile time instead of crashing at runtime.

open as a page

In an Android Fragment, why does the view have its own lifecycle, and what breaks when code ignores viewLifecycleOwner and onDestroyView?

level: middleimportance: should knowfreq 65%

basics

~20 s

A Fragment can outlive its view: replaced with addToBackStack, it stops and runs onDestroyView, then builds a new view on return. Observing with the fragment as owner piles up observers, and a binding kept past onDestroyView leaks the old views.

open as a page

In Android, how do the standard, singleTop, singleTask and singleInstance launch modes differ, and when does an activity get onNewIntent instead of a new instance?

level: middleimportance: should knowfreq 60%

basics

~20 s

standard always makes a new instance; singleTop reuses one only on top of the current task; singleTask keeps one at a task root and clears activities above it; singleInstance keeps it alone in its task. Reuse arrives in onNewIntent().

open as a page

With adb shell am and pm, how do you reset an Android app and relaunch one screen to reproduce a tester's bug repeatably?

level: middleimportance: should knowfreq 45%

basics

~20 s

Use pm for the package and am for the running app: am force-stop and pm clear return it to a fresh-install state, pm grant sets permissions, and am start -n with extras opens the exact screen, so every attempt starts identically.

open as a page

In an Android build, what do an APK, an AAB and an AAR each package, and who consumes each one?

level: middleimportance: should knowfreq 62%

basics

~20 s

An APK is the signed package a device installs and runs; an AAB is the publishing format Google Play turns into signed, per-device APKs; an AAR is an Android library archive that app modules consume as a dependency.

open as a page

In the Android Gradle plugin, how do you give a staging build its own backend URL and application ID so it installs beside production?

level: middleimportance: should knowfreq 55%

basics

~10 s

Add a staging build type with initWith(debug) and applicationIdSuffix ".staging", declare the URL with buildConfigField per build type, enable buildFeatures.buildConfig, and override resources such as the app name in a src/staging source set.

open as a page

On Android, how do you let another app read one file or row from your content provider without exporting it, and what does FLAG_GRANT_READ_URI_PERMISSION grant?

level: middleimportance: should knowfreq 45%

basics

~10 s

Keep the provider unexported, enable android:grantUriPermissions, and send the item's content:// URI in an intent carrying FLAG_GRANT_READ_URI_PERMISSION. The receiving app gets temporary read access to that URI only, with no permission of its own.

open as a page

In the Android Keystore, what do the timeout and type arguments of setUserAuthenticationParameters control, and why did it replace setUserAuthenticationValidityDurationSeconds?

level: middleimportance: should knowfreq 50%

basics

~20 s

With setUserAuthenticationRequired(true), timeout 0 demands authentication for every use through BiometricPrompt and a CryptoObject; a positive timeout allows use for that many seconds after authenticating. Type picks AUTH_BIOMETRIC_STRONG, AUTH_DEVICE_CREDENTIAL or both. API 30 added it, deprecating the seconds-only method.

open as a page

On Android, how should a nearby-posts feature declare and request location permissions, and what must it handle when the user grants only approximate location?

level: middleimportance: should knowfreq 60%

basics

~20 s

Declare ACCESS_COARSE_LOCATION, and ACCESS_FINE_LOCATION only if precision truly helps, then request them together at the moment the feature opens. If the user grants approximate only, the app gets approximate locations whatever it declared, so the feature must work on them.

open as a page

showing 1–30 of 52