Android
Android is an open source and Linux-based operating system for mobile
devices such as smartphones and tablet computers.
Latest version android-Android 12 is the twelfth major release and 19th
version of Android.
App componentsApp components are the essential building blocks of an
Android app. Each component is an entry point through which the system or a
user can enter your app. 4types- activity,services, broadcast receiver, content
provider.
Activities-- An activity is the entry point for interacting with the user. It
represents a single screen with a user interface.
Services-- A service is a general-purpose entry point for keeping an app
running in the background for all kinds of reasons. It is a component that runs in
the background to perform long-running operations or to perform work for
remote processes. There are two 2 types of services that tell the system how to
manage an app: started services and bound services.
Started services tell the system to keep them running until their work is
completed. This could be to sync some data in the background or play music
even after the user leaves the app. Syncing data in the background or playing
music also represent two different types of started services that modify how the
system handles them:
Started
A service is started when an application component, such as an activity, starts
it by calling startService(). Once started, a service can run in the background
indefinitely, even if the component that started it is destroyed.
Bound
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, get results, and
even do so across processes with interprocess communication (IPC).
life cycle when the service is created with startService() and life cycle when
the service is created with bindService().
To create an service, you create a Java class that extends the Service base
class or one of its existing subclasses.
1 onStartCommand()
The system calls this method when another component, such as an activity, requests that the service
be started, by calling startService(). If you implement this method, it is your responsibility to stop the
service when its work is done, by calling stopSelf() or stopService() methods.
2 onBind()
The system calls this method when another component wants to bind with the service by
calling bindService(). If you implement this method, you must provide an interface that clients use to
communicate with the service, by returning an IBinder object. You must always implement this method,
but if you don't want to allow binding, then you should return null.
3 onUnbind()
The system calls this method when all clients have disconnected from a particular interface published
by the service.
4 onRebind()
The system calls this method when new clients have connected to the service, after it had previously
been notified that all had disconnected in its onUnbind(Intent).
5 onCreate()
The system calls this method when the service is first created using onStartCommand() or onBind().
This call is required to perform one-time set-up.
6 onDestroy()
The system calls this method when the service is no longer used and is being destroyed. Your service
should implement this to clean up any resources such as threads, registered listeners, receivers, etc.
Broadcast Receivers simply respond to broadcast messages from other applications or
from the system itself. These messages are sometime called events or intents. For
example, applications can also initiate broadcasts to let other applications know that some
data has been downloaded to the device and is available for them to use, so this is
broadcast receiver who will intercept this communication and will initiate appropriate action.
example, a broadcast announcing that the screen has turned off, the battery is
low, or a picture was captured.
A content provider component supplies data from one application to
others on request.
Content providers let you centralize content in one place and have
many different applications access it as needed. A content provider
behaves very much like a database where you can query it, edit its
content, as well as add or delete content using insert(), update(), delete(),
and query() methods. In most cases this data is stored in
an SQlite database.
A Fragment is a piece of an activity which enable more modular activity
design, a fragment is a kind of sub-activity.
Fragment provides us with 2 major things:
1. Modularity
2. Adaptability
For example — we used Gmail application in our mobile phones, and
also in tablets. Both show UI different.
AdaptabilityWe can also take one example of WhatsApp, to
understand it better. We see three tabs on WhatsApp, swiping which will
give us another tab open up. Chat/Status/Calls .
Library module -> Provides a container for your reusable code, which you can use as a dependency in
other app modules or import into other projects.
When fragment come up on the screen:-
1. onAttach() — This method called first, To know that our fragment has
been attached to an activity. We are passing the Activity that will
host our fragment.
2. onCreate() — This method called when a fragment instance
initializes, just after the onAttach where fragment attaches to the
host activity.
3. onCreateView() — The method called 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.
4. onActivityCreated() — This method called when Activity completes
its onCreate() method
5. onStart() — This method called when a fragment is visible.
6. onResume() — This method called when a fragment is visible and
allowing the user to interact with it. Fragment resumes only after
activity resumes.
When fragment goes out off the screen:-
1. onPause() — This method called when a fragment is not allowing the
user to interact; the fragment will get change with other fragment
or it gets removed from activity or fragment’s activity called a
pause.
2. onStop() — This method called when the fragment is no longer
visible; the fragment will get change with other fragment or it gets
removed from activity or fragment’s activity called stop.
3. onDestroyView() — This method called when the view and related
resources created in onCreateView() are removed from the activity’s
view hierarchy and destroyed.
4. onDestroy() — This method called when the fragment does its final
clean up.
5. onDetach() — This method called when the fragment is detached
from its host activity.
The Application class in Android is the base class within an Android app that
contains all other components such as activities and services.
Res-- Contains all non-code resources, such as XML layouts, UI strings, and bitmap images,
divided into corresponding sub-directories.
Dependencies: Lists the library, file, and module dependencies for this module. You can add, modify, and
delete dependencies from this pane.
ContextIt is the context of the current state of the application.
It can be used to get information regarding the activity and
application.
It can be used to get access to resources, databases, and shared
preferences, and etc.
Both the Activity and Application classes extend the Context class.
Application Context: It is the application and we are present in
Application. For example - MyApplication(which extends Application
class). It is an instance of MyApplication only.
It is an instance that is the singleton and can be accessed in activity
via getApplicationContext()
Activity Context: It is the activity and we are present in Activity.
For example - MainActivity. It is an instance of MainActivity only.
Activities
An activity represents a single screen with a user interface just like window or frame of
Java.Android activity is the subclass of ContextThemeWrapper class.
onCreate()-This is the first callback and called when the activity is first created.
onStart()-This callback is called when the activity becomes visible to the user.
onResume()-This is called when the user starts interacting with the application.
onPause()-The paused activity does not receive user input and cannot execute any code and called
when the current activity is being paused and the previous activity is being resumed.
onStop()--This callback is called when the activity is no longer visible.
onDestroy()-This callback is called before the activity is destroyed by the system.
.onRestart()--This callback is called when the activity restarts after stopping it.
An Intent is a messaging object you can use to request an action from
another app component.
Explicit Intents: If you want communication between the components of
your application only then you can use the Explicit Intents. Explicit Intents
are used to communicate with a particular component of the same
application. For example, if you want to launch an Activity by clicking some
button on the present Activity then you can specify the fully-qualified address
of the desired Activity to launch that Activity.
Implicit Intents: don’t need to specify the fully-qualified address.
For example, you can access the current location by accessing the location
data from other application .
onSavedInstanceState() - This method is used to store data before pausing
the activity.
onRestoreInstanceState() - This method is used to recover the saved state
of an activity when the activity is recreated after destruction.
When should you use a Fragment rather than an Activity?
o When you have some UI components to be used across various
activities
o When multiple view can be displayed side by side just like viewPager
What is the difference between FragmentPagerAdapter vs
FragmentStatePagerAdapter?
o 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.
o FragmentStatePagerAdapter: Here, the fragment instance will be
destroyed when it is not visible to the user, except the saved state of
the fragment.
View: View objects are the basic building blocks of User Interface(UI) elements
in Android. Examples are EditText, Button, CheckBox etc.
ViewGroup: ViewGroup is the invisible container. It holds View and ViewGroup.
For example, LinearLayout is the ViewGroup that contains Button(View), and
other Layouts also.
How to communicate between fragments?
communication between fragments should not be done directly. There
are two ways of doing so.
With the help of ViewModel—
First, we will create a class SharedViewModel.
MessageReceiverFragment: Fragment is going to receive the
message which will be sent by MessageSenderFragment. And will
have a TextView which will show the received message.
MessageSenderFragment: Fragment is going to send the message
which will be received by MessageReceiverFragment. And will have
a Button to send the message.
With the help of Interface
Make an Interface in your FragmentA
Implement the Interface of the FragmentA in your Activity
Call the Interface method from your Activity
In your Activity, call your FragmentB to do the required changes
RecyclerView
RecyclerView is a ViewGroup, which populates a list on a collection of
data provided with the help of ViewHolder and draws it to the user on-
screen.
The major components of RecyclerView are- Adapter ,ViewHolder,
Layout Manager.
Adapter
It is a subtype of RecyclerView.Adapter class. It takes the data set which
has to be displayed to the user in RecyclerView. It is like the main
responsible class to bind the views and display it.
ViewHolder
ViewHolder is a type of a helper class that helps us to draw the UI for
individual items that we want to draw on the screen.
LayoutManager
LayoutManager in recyclerView helps us to find out how we need to
display the items on the screen. Like linearly or in a grid.
IntentFilter objects are often created in XML as part of a
package's AndroidManifest.xml file, using intent-filter tags.
Event HandlingEvents are a useful way to collect data about a user's interaction
with interactive components of Applications.
three concepts related to Android Event Management −
Event Listeners − An event listener is an interface in the View class that contains a single callback
method. These methods will be called by the Android framework when the View to which the
listener has been registered is triggered by user interaction with the item in the UI.
Event Listeners Registration − Event Registration is the process by which an Event Handler gets
registered with an Event Listener so that the handler is called when the Event Listener fires the
event.
Event Handlers − When an event happens and we have registered an event listener for the event,
the event listener calls the Event Handlers, which is the method that actually handles the event.