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.
List View vs RecyclerView
a recycler view is an improved list view.
- recycler view reuses cells while scrolling up and down
- decouples list from its container
- animates common list actions
What is Intent
an intent is a messaging object that can be used to request an action from another app component.
implicit - all you need to do is specify the action that will be performed by the intent and the android system will search all available components that can perform that action
explicit - are used to communicate with a particular component of the same application.
you can use intents to start activities
starting a service
delivering a broadcast
What are Intent filters
they are expressions that are used to specify the type of components or actions that can be received by the application
broadcast receiver
listens for broadcasts and respond to broadcast messages from other applications or from the system
broadcasts
broadcasts sends a message out so any broadcast receiver that is listening
there are 3 ways to send a broadcast
what is a service
an android component that preforms long running tasks in the background that don’t need to provide UI
3 types of services
foreground- is a service that lets the user know what is going on. ie music player
background- the user wont know what is going on like when compressing an image when you send it
bound- you can bind a service to an activity so even if you leave the app the service can still run and when you come back you can take back control of the service
Content Providers
helps applications manage access to data stored by itself, other apps, and share data with other apps. they provide tools for data security.
they are the standard interface that connects data between processes.
you can configure a content provider to allow other applications to securely access and modify your app’s data.
if you don’t plan to share data you can still use content providers to abstract your data
some use cases would be implementing a custom search suggestion in your application using the searchRecentSuggestionsProvider
exposing your data to widgets
to access a content provider you can use the contentResolver
what is an ANR
application not responding happens when no response to an input event within 5 seconds or a broadcast receiver hasn’t finished executing within 10 seconds
intent service
an intent service is a service that is started by a intent. this intent is received on the main thread and then a worker thread is started for the service. tasks are handled on a FIFO bases
JobScheuler
lets your app schedule jobs that won’t be terminated due to the android system.
these jobs can be batched so that the system can use fewer resources.
some good times to use a job scheduler are when
tasks are not critical or user-facing,
tasks that require network access
tasks that should be running on a regular basis
shared preferences and when to use
is a way to store data in the key-value pair
ex for a one time pop up welcome screen
internal storage and when to use
a way to save data the android system will provide some private internal storage only your app can access. when the app is uninstalled the data will be deleted.
you want to use the internal storage when you have private data for your app if the data is being shared it might not be a good idea for internal storage
external storage and when to use
is a way to store data but this data will be accessible to everyone.
you should use external storage when storing a large file or you have shared data. it can also be used if you want some data to persist after deleting the app.
database and when to use
is an organized collection of data stored for future reference.
if you have data that you want to structurally store
shared storage
there is internal storage for apps and then the rest of the storage is shared storage. any app that has storage permissions can access this storage.
when you have data that can be shared
crashes
android vitals can alert you of your apps performance if it has exsesive crashing.
first you need to identify the place where the crash is happening. you can use stack trace to help out with this.
or you should reproduce the the crash locally either by testing the app or reaching out to affected users
a stack trace will show you the type of expectation thrown and the section of code where the exception is thrown.
reproducing the crash in a development environment can be hard to reproduce because the development environment can have more resources. to counteract this use the type of exception to determine what could be the resource that is scarce and limit it.
Live data
LiveData is a data holder that follows the observer design pattern and is lifecycle aware. if an observer is not in an active lifecycle state then it won’t be updated.
the observer will subscribe to the observable and when the observable is updated the observer will be notified.
Work Manager
work manager is the recommended way to schedule background tasks. work manager makes sure the scheduled task will run even if the app or device restarts.
ViewModel
is used to handle configuration changes - objects are kept when an activity is recreated
lifecycle awareness-
data sharing- between fragments is easy
coroutines support
unit tests
a way to evaluate the app’s logic without running tests on a device
bundles
is a way to pass data around between activities
Git
prevents loss of work.
keeps track of the history of changes
you can work independently on a separate branch
you should commit often
make sure you working with the latest version to avoid merge conflicts
make detailed commits
use branches
view binding
allows you to bind ui components in your layouts to data sources in your app.
instead of using find view by id you assign data directly to the widget in the layout file.
okhttp
is a very handy tool we can use for introceptors
constructor vs method
constructor has the same name as the class and methods can be anything there is no return type on the constructor a constructor is called when an object is created
abstract class vs interface
an abstract class is a class that contains both concrete and abstract methods. an abstract method must be implemented by an abstract subclass. an abstract class can not be instantiated but have to be extended to be used
the interface is a blueprint of a class. contains empty methods that will be used by all subclasses. the subclasses will implement those methods.
access modifiers
private- visible inside the class only default- allows access from inside the same package protected - same as private but visible to subclasses public- any client who sees the declaring class sees it