Using Activities, Fragments and Intents in Android: Prof. Shardul Agravat
Using Activities, Fragments and Intents in Android: Prof. Shardul Agravat
• 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.
• 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.
• onStartCommand() : The system calls this method when another component, such as an
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
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
• onUnbind() : The system calls this method when all clients have disconnected from a particular
• onRebind() : The system calls this method when new clients have connected to the service, after it had
• 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.
2. Launch Activity
3. Display web page
• 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.
• The specification can be just a data type (the mimeType attribute), just a URI, or both a data type and a
URI.
• 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
CATEGORY_APP_MARKET This activity allows the user to browse and download new applications.
CATEGORY_BROWSABLE Activities that can be safely invoked from a browser must support this
category.
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
package
A String[] holding e-mail addresses that should be blind carbon copied.
EXTRA_BCC
Extras
Extra Data Usage
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.
• 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.
• 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
• 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
• 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
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.
• 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