Q. What is Context ?
Ans : Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on.
Ans : Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on.
·
Context represents environment data
·
It provides access to things such as databases
Simpler terms (example )
·
It's like access of android activity to the app's resource.
·
It's similar to when you visit a hotel, you want breakfast,
lunch & dinner in the suitable timings, right?
·
There are many other things you like during the time of stay.
How do you get these things?
·
You ask the room-service person to bring these things for you.
·
Here the room-service person is the context considering you are
the single activity and the hotel to be your app, finally the breakfast, lunch
& dinner have to be the resources.
Different methods by which you can
get context
·
getApplicationContext()
·
getContext()
·
getBaseContext()
·
or this (when in the activity class)
Q 2. What is the Android
Application Architecture?
Ans: Android application architecture has the following
components:
1.
Services −
It will perform background functionalities
2.
Intent −
It will perform the inter connection between activities and the data passing
mechanism
3.
Resource Externalization −
strings and graphics
4.
Notification −
light, sound, icon, notification, dialog box and toast
5.
Content Providers −
It will share the data between applications
Q 3 . Lifecycle of an Activity?
Ans:
·
OnCreate(): This is when the view is first created. This is
normally where we create views, get data from bundles etc.
·
OnStart(): Called when the activity is becoming visible to
the user. Followed by onResume() if the activity comes to the foreground, or
onStop() if it becomes hidden.
·
OnResume(): Called when the activity will start interacting
with the user. At this point your activity is at the top of the activity stack,
with user input going to it.
·
OnPause(): Called as part of the activity lifecycle when an
activity is going into the background, but has not (yet) been killed.
·
OnStop(): Called when you are no longer visible to the
user.
·
OnDestroy(): Called when the activity is finishing
·
OnRestart(): Called after your activity has been stopped,
prior to it being started again
Q4 . What’s the difference between onCreate() and onStart()?
·
The onCreate() method is called once during
the Activity lifecycle, either when the application starts, or when the
Activity has been destroyed and then recreated, for example during a
configuration change.
·
The onStart() method is called whenever the
Activity becomes visible to the user, typically after onCreate() or
onRestart().
Q 5. Describe services?
A
Service
is
an application component that can perform long-running operations in the
background, and it doesn't provide a user interface. It can run in the
background, even when the user is not interacting with your application. These
are the three different types of services:
·
Foreground Service: A foreground service
performs some operation that is noticeable to the user. For example, we can use
a foreground service to play an audio track. A Notification must
be displayed to the user.
·
Background Service: A background service
performs an operation that isn’t directly noticed by the user. In Android API
level 26 and above, there are restrictions to using background services and it
is recommended to use WorkManager in
these cases.
·
Bound Service: A service is bound when
an application component binds to it by calling bindService(). A bound service offers
a client-server interface that allows components to interact with the service,
send requests, receive results. A bound service runs only as long as another
application component is bound to it.
Q6. Difference between Service &
Intent Service ?
·
Service
is the base class for Android services that can be extended to create any
service. A class that directly extends Service runs on the main thread so it
will block the UI (if there is one) and should therefore either be used only
for short tasks or should make use of other threads for longer tasks.
·
IntentService
is a subclass of Service that handles asynchronous requests (expressed as
“Intents”) on demand. Clients send requests through startService(Intent) calls.
The service is started as needed, handles each Intent in turn using a worker
thread, and stops itself when it runs out of work.
Q7. Difference between AsyncTasks &
Threads?
·
Thread should be used to
separate long running operations from main thread so that performance is
improved. But it can’t be cancelled elegantly and it can’t handle configuration
changes of Android. You can’t update UI from Thread.
·
AsyncTask can be used to
handle work items shorter than 5ms in duration. With AsyncTask, you can update
UI unlike java Thread. But many long running tasks will choke the performance.
Q8. Difference between Service, Intent
Service, AsyncTask & Threads
·
Android service is a component
that is used to perform operations on the background such as playing music. It
doesn’t has any UI (user interface). The service runs in the background
indefinitely even if application is destroyed.
·
AsyncTask allows you to
perform asynchronous work on your user interface. It performs the blocking
operations in a worker thread and then publishes the results on the UI thread,
without requiring you to handle threads and/or handlers yourself.
·
IntentService is a base class
for Services that handle asynchronous requests (expressed as Intents) on
demand. Clients send requests through startService(Intent) calls; the service
is started as needed, handles each Intent in turn using a worker thread, and
stops itself when it runs out of work.
·
A thread is a single sequential flow of control within a
program. Threads can be thought of as mini-processes running within a main
process.
Q9. What are Handlers?
Handlers are objects for managing
threads. It receives messages and writes code on how to handle the message.
They run outside of the activity’s lifecycle, so they need to be cleaned up
properly or else you will have thread leaks.
·
Handlers
allow communicating between the background thread and the main thread.
·
A
Handler class is preferred when we need to perform a background task repeatedly
after every x seconds/minutes.
Q10. Difference between Serializable
and Parcelable?
Serialization is the process of
converting an object into a stream of bytes in order to store an object into memory,
so that it can be recreated at a later time, while still keeping the object’s
original state and data.
How to
disallow serialization? We can
declare the variable as transient.
Serializable is a standard Java
interface. Parcelable is an Android specific interface where you implement the
serialization yourself. It was created to be far more efficient than
Serializable (The problem with this approach is that reflection is used and it
is a slow process. This mechanism also tends to create a lot of temporary
objects and cause quite a bit of garbage collection.).
33. Difference between Activity &
Service?
Activities are basically
containers or windows to the user interface. Services is a component that is
used to perform operations on the background. It does not have an UI.
Q11. What is a Sticky Intent?
Sticky Intents allows
communication between a function and a service. sendStickyBroadcast() performs
a sendBroadcast(Intent) known as sticky, i.e. the Intent you are sending stays
around after the broadcast is complete, so that others can quickly retrieve
that data through the return value of registerReceiver(BroadcastReceiver,
IntentFilter). For example, if you take an
intent for ACTION_BATTERY_CHANGED to get battery change events: When you call
registerReceiver() for that action — even with a null BroadcastReceiver — you
get the Intent that was last
Broadcast for that action. Hence, you can use this to find the state of
the battery without necessarily registering for all future state changes in the
battery.
Q12. What is a Pending Intent?
If you want someone to perform
any Intent operation at future point of time on behalf of you, then we will use
Pending Intent.
Q13. What is an Action?
Description of the intent. For
instance, ACTION_CALL — used to perform calls
Q14. What are intent Filters?
Specifies the type of intent that
the activity/service can respond to.
Q15. Describe fragments?
Fragment is a UI entity attached
to Activity. Fragments can be reused by attaching in different activities.
Activity can have multiple fragments attached to it. Fragment must be attached
to an activity and its lifecycle will depend on its host activity.
Q16. Describe fragment lifecycle ?
·
onAttach() : The fragment instance is associated with an
activity instance.The fragment and the activity is not fully initialized.
Typically you get in this method a reference to the activity which uses the
fragment for further initialization work.
·
onCreate() : The system calls this method when creating
the fragment. You should initialize essential components of the fragment that
you want to retain when the fragment is paused or stopped, then resumed.
·
onCreateView() : The system calls this callback when it’s
time for the fragment to draw its user interface for the first time. To draw a
UI for your fragment, you must return a View component from this method that is
the root of your fragment’s layout. You can return null if the fragment does
not provide a UI.
·
onActivityCreated() : The onActivityCreated() is called after the
onCreateView() method when the host activity is created. Activity and fragment
instance have been created as well as the view hierarchy of the activity. At
this point, view can be accessed with the findViewById() method. example. In
this method you can instantiate objects which require a Context object
·
onStart() : The onStart() method is called once the
fragment gets visible.
·
onResume() : Fragment becomes active.
·
onPause() : The system calls this method as the first
indication that the user is leaving the fragment. This is usually where you
should commit any changes that should be persisted beyond the current user
session.
·
onStop() : Fragment going to be stopped by calling
onStop()
·
onDestroyView() : Fragment view will destroy after call this
method
·
onDestroy() :called to do final clean up of the
fragment’s state but Not guaranteed to be called by the Android platform.
Q17. When should you use a fragment rather
than an activity?
·
When
there are ui components that are going to be used across multiple activities.
·
When
there are multiple views that can be displayed side by side (viewPager tabs)
·
When
you have data that needs to be persisted across Activity restarts (such as
retained fragments)
Q18. Difference between MVC & MVP
& MVVM?
MVC is
the Model-View-Controller architecture
where model refers to the data model classes. The view refers to the xml files
and the controller handles the business logic. The issue with this architecture
is unit testing. The model can be easily tested since it is not tied to
anything. The controller is tightly coupled with the android apis making it
difficult to unit test. Modularity & flexibility is a problem since the
view and the controller are tightly coupled. If we change the view, the
controller logic should also be changed. Maintenance is also an issues.
MVP
architecture: Model-View-Presenter architecture. The View includes the xml and
the activity/fragment classes. So the activity would ideally implement a view
interface making it easier for unit testing (since this will work without a
view).
MVVM:
Model-View-ViewModel Architecture. The Model comprises data, tools
for data processing, business logic. The View Model is responsible for wrapping
the model data and preparing the data for the view. IT also provides a hook to
pass events from the view to the model.
Q19. Android Notification ?
Android Toast class
provides a handy way to show users alerts but problem is that these alerts are
not persistent which means alert flashes on the screen for a few seconds and
then disappears.
Android notification
message fills up the void in such situations. Android notification is a message
that we can display to the user outside of our application’s normal UI.
Notifications in android are built using
NotificationCompat
library.
No comments:
Post a Comment