Android Development
104 flashcards by MentisLibrary
Building Android apps in Kotlin: app components and the manifest, activity and fragment lifecycles, views and Jetpack Compose, ViewModel and coroutines, Room and networking, permissions and services, Gradle and testing.
- What are the four Android app components?
- Activity, Service, BroadcastReceiver and ContentProvider. Each is an entry point the system can start independently, and each must be declared in the manifest.
- What goes in AndroidManifest.xml?
- The package-level declarations the system reads before running any code: the app components and their intent filters, permissions the app requests, hardware and software features it needs, and application-wide attributes such as the theme and the Application class.
- minSdk, targetSdk and compileSdk: what is the difference?
- minSdk is the oldest Android version the app installs on. targetSdk is the version the app has been tested against and whose behaviour changes it opts into. compileSdk is the API level the code is compiled against.
- What is the difference between an APK and an Android App Bundle?
- An APK is the installable package. An AAB is a publishing format containing all the app's code and resources; Google Play generates and signs the specific APKs each device needs from it.
- How does Android choose which resource file to use?
- By matching the device's configuration against qualifiers in the directory name, such as language, screen density, orientation, smallest width and night mode, falling back to the unqualified folder.
- The R class
- A generated class holding an integer id for every resource, so code refers to resources by a compile-checked symbol rather than a file name.
- dp vs sp vs px
- dp is a density-independent pixel that scales with screen density so a view is the same physical size everywhere. sp does the same but also scales with the user's font size preference. px is a raw device pixel.
- Where should app strings live, and why?
- In res/values/strings.xml, referenced as R.string.name. Translations go in values-<language> folders and the system picks the right one.
- What are build types and product flavors?
- Build types describe how to build, typically debug and release, and control debuggability, shrinking and signing. Product flavors describe what to build, such as free and paid. Their combinations are build variants.
- When does the Application class run, and what belongs in it?
- A single object created before any component and living for the whole process. It is where process-wide initialization goes, and where a dependency graph is usually rooted.
- What are the main Context types, and which should you keep?
- Application context lives for the whole process. An Activity context is tied to one screen and carries its theme and window. Keep only the application context in anything long-lived.
- Intent
- A message object describing an operation to perform. An explicit intent names the exact component to start; an implicit intent describes an action and data and lets the system find a component whose intent filter matches.
- What does an <intent-filter> declare?
- Which implicit intents a component is willing to handle, expressed as an action, optional categories and an optional data pattern of scheme, host and path.
- Name the Activity lifecycle callbacks in order
- onCreate, onStart, onResume on the way up; onPause, onStop, onDestroy on the way down. onRestart runs before onStart when returning to a stopped activity.
- Which lifecycle callback should release a resource the user cannot see?
- onStop for anything expensive that should survive a brief interruption, and onPause only for work that must stop the instant the screen loses focus, since onPause must return quickly.
- What happens on a configuration change such as a rotation?
- By default the activity is destroyed and recreated with the new configuration, so resources for the new state are loaded. Instance state saved in onSaveInstanceState is handed back to onCreate.
- When does onSaveInstanceState run, and what belongs in it?
- Saving a small amount of transient UI state into a Bundle before the activity may be destroyed, so it can be restored after a recreation or after the process is killed in the background.
- The task back stack
- A stack of activities representing one coherent piece of work. Starting an activity pushes it on; the Back gesture pops it off, and popping the last one leaves the task.
- What do the activity launch modes do?
- standard creates a new instance every time. singleTop reuses the instance if it is already at the top. singleTask keeps one instance in its own task and clears anything above it. singleInstance keeps it alone in a task.
- How do you get a result back from another activity?
- With the Activity Result APIs: register a contract before the activity is started, then launch it and receive the result in the callback.
- What is a Fragment, and how does its lifecycle relate to its host?
- A reusable portion of a screen with its own lifecycle, hosted inside an activity or another fragment and managed by a FragmentManager. Its view is created and destroyed independently of the fragment instance.
- Why is viewLifecycleOwner needed in a Fragment?
- Because the fragment outlives its view when it is put on the back stack. Observing with the fragment as the owner keeps a reference to the destroyed view and can update it after onDestroyView.
- View binding
- A generated binding class per layout, giving type-safe and null-safe references to every view with an id, replacing findViewById casts.
- What problem does ConstraintLayout solve?
- It positions views relative to each other and to the parent with constraints, so complex screens are built in a single flat hierarchy instead of nested layouts.
- How does RecyclerView work?
- It renders a long list by keeping only enough item views to fill the screen and recycling them as the user scrolls. A LayoutManager positions items, an Adapter creates and binds ViewHolder objects.
- The ViewHolder pattern
- Caching the child view references for one row inside an object, so binding a recycled row does not repeat the view lookups.
- What do ListAdapter and DiffUtil give you?
- DiffUtil computes the minimal set of changes between the old and new lists on a background thread, and ListAdapter applies them as fine-grained item animations.
- What is a theme, and how does it differ from a style?
- A style is a set of attributes applied to a single view. A theme is a set of attributes applied to a whole activity or app, which views and styles then read through attribute references.
- How do you support dark mode?
- Define colours and themes in values and override them in values-night, use theme attributes rather than literal colours, and let AppCompatDelegate follow the system setting.
- @Composable function
- A function annotated @Composable that describes a piece of UI. It has no return value; it emits UI into the composition and can be re-executed whenever the state it reads changes.