100% found this document useful (1 vote)
175 views

Using Activities, Fragments and Intents in Android: Prof. Shardul Agravat

The document discusses key concepts in Android such as activities, fragments, intents, services and their lifecycles. It explains that an activity represents a single screen in an Android app and has a lifecycle consisting of callbacks like onCreate(), onStart(), onResume() etc. It describes services as long-running background tasks and their lifecycle methods like onStartCommand(), onBind() etc. Intents are defined as messaging objects that allow communication between app components, and can be explicit or implicit. Categories and data can be added to intents to refine their targets.

Uploaded by

Dencey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
175 views

Using Activities, Fragments and Intents in Android: Prof. Shardul Agravat

The document discusses key concepts in Android such as activities, fragments, intents, services and their lifecycles. It explains that an activity represents a single screen in an Android app and has a lifecycle consisting of callbacks like onCreate(), onStart(), onResume() etc. It describes services as long-running background tasks and their lifecycle methods like onStartCommand(), onBind() etc. Intents are defined as messaging objects that allow communication between app components, and can be explicit or implicit. Categories and data can be added to intents to refine their targets.

Uploaded by

Dencey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 2

Using Activities, Fragments


and Intents in Android
Prof. Shardul Agravat
Activity

• An activity represents a single screen with a user interface just like


window or frame of Java.Android activity.

• Android system initiates its program with in an Activity starting with a


call on onCreate() callback method.
Android-
Activity
Life Cycle
Android- Activity Life Cycle
• 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.
Android- Activity
• 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.
Service
• A service is a component that runs in the background to perform long-running
operations without needing to interact with the user and it works even if application is
destroyed.

• A service can essentially take two states


• 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 interprocess communication (IPC).
Android- Services
Life Cycle
Android- Services

• 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.

• 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.
Android- Services
• 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.

• onUnbind() : The system calls this method when all clients have disconnected from a particular

interface published by the service.

• 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).

• 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.
What is intent?

The intent is a messaging object which passes between components like services,
content providers, activities, etc. Normally startActivity() method is used for invoking
any activity.

Some of the general functions of intent are:


1. Start service

2. Launch Activity
3. Display web page

4. Display contact list

5. Message broadcasting etc.


Types of Intents

• Explicit Intent : Using explicit intent the targeted component is specified. So


only the specified target component will be invoked.

• Implicit Intent : Using implicit Intent, components can’t be specified. An


action to be performed is declared by implicit intent. Then android operating
system will filter out components that will respond to the action. 
• For Example, as you type the name of your desired webpage and click on the button.
Your webpage is opened.
Explicit Intents
Implicit Intent
Methods of Intent
Method Usage
Context.startActivity() This is to launch a new activity or get an existing
activity to be action.
Context.startService() This is to start a new service or deliver instructions for
an existing service.
Context.sendBroadcast() This is to deliver the message to broadcast receivers.
Intent Objects
• An Intent object is a bundle of information which is used by the component that receives the intent as
well as information used by the Android system.

• An Intent object can contain the following components based on what it is communicating or going to
perform
• Action
• Data
• Category
• Extras
• Flags
• Component Name
Intent Objects

• Action :
• This is mandatory part of the Intent object.

• It is a string naming the action to be performed — or, in the case of broadcast


intents, the action that took place and is being reported.
• The action largely determines how the rest of the intent object is structured .

• The Intent class defines a number of action constants corresponding to


different intents.
Intent standard Action / Activity Action Intent
Activity Action Intent Usage
ACTION_ALL_APPS List all the applications available on the device.
ACTION_ANSWER Handle an incoming phone call.
ACTION_BATTERY_CHANGED This is a sticky broadcast containing the charging state, level, and other
information about the battery.
ACTION_BATTERY_LOW This broadcast corresponds to the "Low battery warning" system dialog.
ACTION_BATTERY_OK This will be sent after ACTION_BATTERY_LOW once the battery has gone back
up to an okay state.
ACTION_BOOT_COMPLETED This is broadcast once, after the system has finished booting.
ACTION_BUG_REPORT Show activity for reporting a bug.
Intent standard Action / Activity Action Intent
Activity Action Intent Usage
ACTION_CALL Perform a call to someone specified by the data.
ACTION_CALL_BUTTON The user pressed the "call" button to go to the dialer or other
appropriate UI for placing a call.
ACTION_CAMERA_BUTTON The "Camera Button" was pressed.
ACTION_CHOOSER Display an activity chooser, allowing the user to pick what they want to
before proceeding.
ACTION_CONFIGURATION_CHANGED The current device Configuration (orientation, locale, etc) has changed.
ACTION_DATE_CHANGED The date has changed.
And many more available on https://www.tutorialspoint.com/android/android_intent_standard_actions.htm
Intent Objects
• Data :
• Adds a data specification to an intent filter.

• The specification can be just a data type (the mimeType attribute), just a URI, or both a data type and a
URI.

• A URI is specified by separate attributes for each of its parts.

• These attributes that specify the URL format are optional, but also mutually dependent.

• If a scheme is not specified for the intent filter, all the other URI attributes are ignored.

• If a host is not specified for the filter, the port attribute and all the path attributes are ignored.

• The setData() method specifies data only as a URI, setType() specifies it only as a MIME type, and
setDataAndType() specifies it as both a URI and a MIME type. The URI is read by getData() and the type by
getType().
Action / Data Pair
Action / Data Pair Usage

ACTION_VIEW content://contacts/people/1 Display information about the person whose identifier is "1".

ACTION_DIAL content://contacts/people/1 Display the phone dialler with the person filled in.

ACTION_VIEW tel:123 Display the phone dialer with the given number filled in.

ACTION_DIAL tel:123 Display the phone dialer with the given number filled in.

ACTION_EDIT content://contacts/people/1 Edit information about the person whose identifier is "1".

ACTION_VIEW content://contacts/people/ Display a list of people, which the user can browse through.
Action / Data Pair
Action / Data Pair Usage

ACTION_SET_WALLPAPER Show settings for choosing wallpaper

It going to be synchronize the data


ACTION_SYNC

It will start the platform-defined tutorial.


ACTION_SYSTEM_TUTORIAL

It intimates when time zone has changed


ACTION_TIMEZONE_CHANGED

It is used to run default uninstaller


ACTION_UNINSTALL_PACKAGE
Intent Objects
• Category :
• The category is an optional part of Intent object and it's a string containing
additional information about the kind of component that should handle the
intent.

• The addCategory() method places a category in an Intent object.

• removeCategory() deletes a category previously added.

• getCategories() gets the set of all categories currently in the object.


Category
Category Usage

CATEGORY_APP_BROWSER Used with ACTION_MAIN to launch the browser application.

CATEGORY_APP_CALCULATOR Used with ACTION_MAIN to launch the calculator application.

CATEGORY_APP_CALENDAR Used with ACTION_MAIN to launch the calendar application.

CATEGORY_APP_CONTACTS Used with ACTION_MAIN to launch the contacts application.

CATEGORY_APP_EMAIL Used with ACTION_MAIN to launch the email application.

CATEGORY_APP_GALLERY Used with ACTION_MAIN to launch the gallery application.

CATEGORY_APP_MAPS Used with ACTION_MAIN to launch the maps application.


Category
Category Usage

CATEGORY_APP_MARKET This activity allows the user to browse and download new applications.

CATEGORY_APP_MESSAGING Used with ACTION_MAIN to launch the messaging application.

CATEGORY_APP_MUSIC Used with ACTION_MAIN to launch the music application.

CATEGORY_BROWSABLE Activities that can be safely invoked from a browser must support this
category.

CATEGORY_CAR_DOCK An activity to run when device is inserted into a car dock.

And many more as available on :

https://www.tutorialspoint.com/android/android_intent_standard_categories.htm
Intent Objects
• Extras :
• This will be in key-value pairs for additional information that should be
delivered to the component handling the intent.

• The extras can be set and read using the putExtras() and getExtras() methods
respectively.
Extras
Extra Data Usage

EXTRA_ALARM_COUNT Used as an int extra field in AlarmManager intents to tell the application being

invoked how many pending alarms are being delivered with the intent.

EXTRA_ALLOW_MULTIPLE Used to indicate that a ACTION_GET_CONTENT intent can allow the user to

select and return multiple items.

EXTRA_ALLOW_REPLACE Used as a boolean extra field with ACTION_INSTALL_PACKAGE to install a

package
A String[] holding e-mail addresses that should be blind carbon copied.
EXTRA_BCC
Extras
Extra Data Usage

EXTRA_CC A String[] holding e-mail addresses that should be carbon copied.

EXTRA_EMAIL A String[] holding e-mail addresses that should be delivered to.

EXTRA_INTENT An Intent describing the choices you would like shown with

ACTION_PICK_ACTIVITY.

EXTRA_SHORTCUT_ICON The name of the extra used to define the icon, as a Bitmap, of a shortcut.

EXTRA_SHORTCUT_INTENT The name of the extra used to define the Intent of a shortcut.

EXTRA_SHORTCUT_NAME The name of the extra used to define the name of a shortcut.

And many other as available on : https://www.tutorialspoint.com/android/android_intent_standard_extra_data.htm


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.


Fragments
• 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.

• Fragments were added to the Android API in Honeycomb version of Android which API
version 11.

• 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.
Fragments
Fragment Life Cycle
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 a reference to the activity in this method, 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.


List of methods which you can to override in your fragment class

• 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.

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.


List of methods which you can to override in your fragment class

• 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 of 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.

You might also like