Component Flashcards
What is the purpose of a ViewModel?
It is used to store and manage UI-related data.
How does a ViewModel handle configuration changes?
It retains UI-related data across configuration changes.
How is ViewModel lifecycle-aware?
When its lifecycle owner is destroyed, it cleans up its data.
How does ViewModel help with application architecture?
It helps separate business logic from the View.
How does each UI component manage its ViewModel internally?
Each UI component, like an activity or fragment, has a ViewModelStore that stores its ViewModels.
Is it possible to force GC?
No, it just makes a request to the system, but it doesn’t guarantee that it will happen
What does launch mode define in Android?
Launch mode defines how an activity is launched.
What happens when the launch mode is set to SingleTask in Android?
If the activity already exists in the stack, it reuses it instead of creating a new one, and any activities above it in the stack are destroyed.
What is called when an activity is reused in SingleTask launch mode in Android?
Once the activity is reused, the onNewIntent method is called.
Why can’t a Fragment have constructor parameters in Android?
Because Android uses reflection to instantiate a Fragment with a no-argument constructor.
Why does setContentView need to be called in onCreate in Android?
Since setContentView is costly, it should be placed in onCreate() which is called only once during the activity lifecycle.
What causes App Lag?
- Frequent Garbage Collection (GC)
- Too much work being done on the main thread
What are the solutions to reduce App Lag?
- Use lazy initialization to defer initialization until needed
- Declare commonly used variables as static to prevent memory allocation overhead
- Move time-consuming tasks to a background thread
What’s Context in Android?
It provides access to app resources, system services, and environment information.
What’s ApplicationContext in Android?
- It’s tied to the app lifecycle and remains alive as long as the app is running.
- Use it when you need a long-lived context.
- Use the Activity context when you want to perform UI operations.
How to communicate between two Fragments?
- Both fragments share the same ViewModel.
- The hosting activity acts as a mediator to pass data.
What’s the retained Fragment in Android?
The fragment persists across configuration changes by calling setRetainInstance(true).
What’s the purpose of addToBackStack in Android?
- It’s used to add a transaction to the back stack, enabling the reversal of the transaction.
- Allows the user to return to the previous fragment when pressing the back button.
What’s the difference between View and ViewGroup in Android?
- View: A single UI element that displays content and interacts with the user.
- ViewGroup: A container that holds multiple Views and other ViewGroups.
What is the key performance advantage of SurfaceView over TextureView?
SurfaceView runs on a background thread, offering better performance than TextureView.
How does SurfaceView render content on the screen?
SurfaceView renders on a separate surface and overlays the main UI.
What is the key feature of TextureView regarding content transformations?
TextureView supports content transformations such as moving, resizing, and rotating.
On which thread does TextureView run?
TextureView runs on the main thread.
What is RecyclerView used for?
RecyclerView is an efficient view for displaying a large dataset.