Andriod for Final-term -1
Andriod for Final-term -1
In Android, it is quite usual for users to witness a jump from one application to another
as a part of the whole process, for example, searching for a location on the browser
and witnessing a direct jump into Google Maps or receiving payment links in
Messages Application (SMS) and on clicking jumping to PayPal or GPay (Google
Pay). This process of taking users from one application to another is achieved by
passing the Intent to the system.
Intents, in general, are used for navigating among various activities within the same
application, but note, is not limited to one single application, i.e., they can be utilized
from moving from one application to another as well.
Intents could be Implicit, for instance, calling intended actions, and explicit as well,
such as opening another activity after some operations like onClick or anything else.
Below are some applications of Intents:
Syntax:
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.geeksforgeeks.org/"));
startActivity(intent);
Explicit Intent
Explicit Intent specifies the component. In such a case, intent provides the external
class to be invoked.
Syntax:
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in
with built in SQLite database implementation.
SQLite supports all the relational database features. In order to access this database, you don't need to
establish any kind of connections for it like JDBC,ODBC e.t.c
Database - Package
The main package is android.database.sqlite that contains the classes to manage your own databases
Database - Creation
In order to create a database you just need to call this method openOrCreateDatabase with your
database name and mode as a parameter. It returns an instance of SQLite database which you have to
receive in your own object.Its syntax is given below
Apart from this , there are other functions available in the database package , that does this job. They
are listed below
Database - Insertion
we can create table or insert data into table using execSQL method defined in SQLiteDatabase class.
Its syntax is given below
This will insert some values into our table in our database. Another method that also does the same
job but take some additional parameter is given below
Database - Fetching
We can retrieve anything from database using an object of the Cursor class. We will call a method of
this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can
move the cursor forward and retrieve the data.
There are other functions available in the Cursor class that allows us to effectively retrieve the data.
That includes
1 getColumnCount()
This method return the total number of columns of the table.
2 getColumnIndex(String columnName)
This method returns the index number of a column by specifying the name of the column
3 getColumnName(int columnIndex)
This method returns the name of the column by specifying the index of the column
4 getColumnNames()
This method returns the array of all the column names of the table.
5 getCount()
This method returns the total number of rows in the cursor
6 getPosition()
This method returns the current position of the cursor in the table
7 isClosed()
This method returns true if the cursor is closed and return false otherwise
For managing all the operations related to the database , an helper class has been given and is called
SQLiteOpenHelper. It automatically manages the creation and update of the database. Its syntax is
given below
Started
A service is started when an application component, such as an activity, starts it by
1
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
2 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).
A service has life cycle callback methods that you can implement to monitor changes in the service's
state and you can perform work at the appropriate stage. The following diagram on the left shows the
life cycle when the service is created with startService() and the diagram on the right shows the life
cycle when the service is created with bindService(): (image courtesy : android.com )
To create an service, you create a Java class that extends the Service base class or one of its existing
subclasses. The Service base class defines various callback methods and the most important are
given below. You don't need to implement all the callbacks methods. However, it's important that
you understand each one and implement those that ensure your app behaves the way users expect.
onStartCommand()
The system calls this method when another component, such as an activity, requests that the
1 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.
onBind()
2 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.
onUnbind()
3 The system calls this method when all clients have disconnected from a particular interface
published by the service.
onRebind()
4 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).
onCreate()
5 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.
onDestroy()
The system calls this method when the service is no longer used and is being destroyed.
6
Your service should implement this to clean up any resources such as threads, registered
listeners, receivers, etc.
Android Fragments
A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong
if we say, a fragment is a kind of sub-activity.
A fragment has its own layout and its own behaviour with its own life cycle callbacks.
You can add or remove fragments in an activity while the activity is running.
You can combine multiple fragments in a single activity to build a multi-pane UI.
A fragment can be used in multiple activities.
Fragment life cycle is closely related to the life cycle of its host activity which means when the
activity is paused, all the fragments available in the activity will also be stopped.
A fragment can implement a behaviour that has no user interface component.
You create fragments by extending Fragment class and You can insert a fragment into your activity
layout by declaring the fragment in the activity's layout file, as a <fragment> element.
Prior to fragment introduction, we had a limitation because we can show only a single activity on the
screen at one given point in time. So we were not able to divide device screen and control different
parts separately. But with the introduction of fragment we got more flexibility and removed the
limitation of having a single activity on the screen at a time. Now we can have a single activity but
each activity can comprise of multiple fragments which will have their own layout, events and
complete life cycle.
Following is a typical example of how two UI modules defined by fragments can be combined into
one activity for a tablet design, but separated for a handset design.
The application can embed two fragments in Activity A, when running on a tablet-sized device.
However, on a handset-sized screen, there's not enough room for both fragments, so Activity A
includes only the fragment for the list of articles, and when the user selects an article, it starts
Activity B, which includes the second fragment to read the article.
Android fragments have their own life cycle very similar to an android activity. This section briefs
different stages of its life cycle.
Fragment lifecycle
Here is the list of methods which you can to override in your fragment class −
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()onDestroy() called to do final clean up of the fragment's state but Not guaranteed
to be called by the Android platform.
How to use Fragments?
First of all decide how many fragments you want to use in an activity. For example let's we
want to use two fragments to handle landscape and portrait modes of the device.
Next based on number of fragments, create classes which will extend the Fragment class. The
Fragment class has above mentioned callback functions. You can override any of the functions
based on your requirements.
Corresponding to each fragment, you will need to create layout files in XML file. These files
will have layout for the defined fragments.
Finally modify activity file to define the actual logic of replacing fragments based on your
requirement.
Types of Fragments
Single frame fragments − Single frame fragments are using for hand hold devices like mobiles,
here we can show only one fragment as a view.
List fragments − fragments having special list view is called as list fragment
Fragments transaction − Using with fragment transaction. we can move one fragment to
another fragment.