Core Android Flashcards
What is Context? How is it used?
The context can be simply explained as where we are currently at or describing
it can be used to get information regarding the activity and application
it can also be used to get access to resources, databases, and shared preferences
Application Context
used when you need a context that will outlive any other context.
Activity Context
represents the activity and lifecycle useful for UI operations
What are Activity and its lifecycle?
an activity is the entry point for an app and provides a window for the UI.
- onCreate
- Called when activity is first created.
- onStart
- Called when activity is becoming visible to the user.
- onResume
- Called when activity will start interacting with the user.
- onPause
- Called when activity is not visible to the user.
- onStop
- Called when activity is no longer visible to the user.
- onRestart
- Called after your activity is stopped, prior to starting.
- onDestroy
- Called before the activity is destroyed.
why do we need to call setContentView() in onCreate() of activity class?
onCreate is called only once when the activity starts. so calling the setContentView inflates the UI
What is onSavedInstanceState() and onRestoreInstanceState()
onSaved will be called before pausing the activity
onRestore will be used to recover the saved state when the activity is recreated
what is a fragment and its lifecycle
a fragment is a sub-activity that has its own layout
It added from an activity
has its own behavior and lifecycle but is closely related to the host activity lifecycle
fragment will be used if you need to have multiple views displayed beside each other or reuse the display
what are launch modes
they launch activities with a certain set of instructions that can help with the task
standard - creates a new instance of the activity everytime
singletop - if the activity is already on top of the stack a new instance will not be created otherwise it will create a new activity
single task- only creates one instance of the activity if the activity is already in the stack then it will destroy the activities in front of it
single instance- will create a new stack for the activity so whenever there is a new instance of that activity it will get it from that stack
what is the differnce between FragmentPageAdapter vs FragmentStatePagerAdaper
FragmentPagerAdapter- Each fragment visited by the user will be stored in the memory but the view will be destroyed when the page is revisited then the view will be created not the instance of the fragment.
FragmentStatePagerAdapter- the fragment instance will be destroyed when it’s not visible to the user, except the saved state of the fragment
What is the difference between adding/ replacing fragment in backstack?
add to back stack means the transaction will be remembered and will be reversed when pupped off the stack
add will add a fragment to the activity state
why is it recommended to use only the default constructor to create a fragment
when android has to restore a fragment it will use the default constructor like when an orientation change occurs.
switch vs when in kotlin
the when statement in kotlin is more powerful then the switch statement.
-you can use more then one choice in a case
-you can add conditions to cases
-you can pass any type
finally when doesn’t need an else statement and will break on the first case it finds
what is inheritance
inherit properties from other classes
superclass vs subclass
superclass is is a parent class or base class so the subclass will inherit from the base class
polymorphism
multiple definitions can be given to a single interface
used when different subclasses need need different methods
polymorphism
multiple definitions can be given to a single interface
used when different subclasses need different methods
overloading vs overriding
overloading has two or more methods have the same name but different parameters overriding a child class redefines a methods in the base class
how to communicate between two fragments
you can use a shared ViewModel
observing livedata in one fragment and changing live data in another
or by interfaces
creating an interface in one fragment implement that interface in the activity and then call a method in the second fragment.
View.GONE vs View.INVISIBLE
View.GONE will make it so the view is removed from the layout
View.INVISIBLE will make it so the view still takes up space in the layout but you can’t see the view
Constraint layout
It is a flexible layout that can adapt to different screen sizes. they can avoid the nested layouts which leads to less complex layouts.