0% found this document useful (0 votes)
57 views22 pages

ITE 2152 Introduction To Mobile Application Development: Week 7

The document discusses the lifecycle of activities and fragments in Android. It explains the different callback methods for activities like onCreate(), onStart(), onResume(), etc. and describes what happens when each method is called. It also discusses the different callback methods for fragments like onAttach(), onCreate(), onCreateView(), onActivityCreated(), etc. and explains the fragment lifecycle. It provides an example of how to create fragments by extending the Fragment class and including them in an activity layout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views22 pages

ITE 2152 Introduction To Mobile Application Development: Week 7

The document discusses the lifecycle of activities and fragments in Android. It explains the different callback methods for activities like onCreate(), onStart(), onResume(), etc. and describes what happens when each method is called. It also discusses the different callback methods for fragments like onAttach(), onCreate(), onCreateView(), onActivityCreated(), etc. and explains the fragment lifecycle. It provides an example of how to create fragments by extending the Fragment class and including them in an activity layout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

ITE 2152

Introduction to Mobile
Application Development
Week 7

M P C Sandaru – Software Engineer


Lifecycle of an activity
Events of Activity class
• onCreate()—Called when the activity is first created
• onStart()—Called when the activity becomes visible to the
user
• onResume()—Called when the activity starts interacting
with the user
• onPause()—Called when the current activity is being
paused and the previous activity is being resumed
• onStop()—Called when the activity is no longer visible to
the user
• onDestroy()—Called before the activity is destroyed by the
system (either manually or by the system to conserve
memory)
• onRestart()—Called when the activity has been stopped
and is restarting again
Intent
• An Android Intent is an abstract description of
an operation to be performed.
• It can be used with startActivity to launch an
Activity, broadcastIntent to send it to any
interested BroadcastReceiver components,
and startService(Intent) or bindService(Intent,
ServiceConnection, int) to communicate with
a background Service.
Example
• For example, let's assume that you have an Activity that needs to
launch an email client and sends an email using your Android device.
For this purpose, your Activity would send an ACTION_SEND along
with appropriate chooser, to the Android Intent Resolver. The specified
chooser gives the proper interface for the user to pick how to send
your email data.

Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));


email.putExtra(Intent.EXTRA_EMAIL, recipients);
email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
startActivity(Intent.createChooser(email, "Choose an email client
from..."));
Output
Methods of intent

Sr.No Method & Description


Context.startActivity()
1 The Intent object is passed to this method to launch a new activity or
get an existing activity to do something new.
Context.startService()
2 The Intent object is passed to this method to initiate a service or deliver
new instructions to an ongoing service.
Context.sendBroadcast()
3 The Intent object is passed to this method to deliver the message to all
interested 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.
Action
• This is mandatory part of the Intent object and 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. Here is a list of Android Intent
Standard Actions
• The action in an Intent object can be set by the setAction() method
and read by getAction().
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.
• 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, and getCategories() gets the set of all categories
currently in the object.
Sr.No. Action/Data Pair & Description
ACTION_VIEW content://contacts/people/1 Display information about the person whose
1
identifier is "1".
ACTION_DIAL content://contacts/people/1 Display the phone dialer with the person filled
2
in.

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

4 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


5
identifier is "1".
ACTION_VIEW content://contacts/people/ Display a list of people, which the user can
6
browse through.

7 ACTION_SET_WALLPAPER Show settings for choosing wallpaper

ACTION_SYNC It going to be synchronous the data,Constant Value is


8
android.intent.action.SYNC
ACTION_SYSTEM_TUTORIAL It will start the platform-defined tutorial(Default tutorial or
9
start up tutorial)

10 ACTION_TIMEZONE_CHANGED It intimates when time zone has changed

11 ACTION_UNINSTALL_PACKAGE It is used to run default uninstaller


Types of Intent
• There are following two types of intents
supported by Android
Explicit Intents
• Explicit intent going to be connected internal
world of application, suppose if you wants to
connect one activity to another activity, we can
do this quote by explicit intent, below image is
connecting first activity to second activity by
clicking button.
Implicit Intents
• These intents do not name a target and the field
for the component name is left blank. Implicit
intents are often used to activate components in
other applications. For example −
Intent read1=new Intent();
read1.setAction(android.content.Intent.ACTION_VIEW);
read1.setData(ContactsContract.Contacts.CONTENT_URI);
startActivity(read1);
Intent Filters
• Android OS uses filters to pinpoint the set of
Activities, Services, and Broadcast receivers
that can handle the Intent with help of
specified set of action, categories, data
scheme associated with an Intent. You will use
<intent-filter> element in the manifest file to
list down actions, categories and data types
associated with any activity, service, or
broadcast receiver.
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.

Following are important points about fragment −


• 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.
• Fragments were added to the Android API in Honeycomb version of
Android which API version 11.
Fragments
• 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.
Fragment Life Cycle
• Android fragments have their own life cycle
very similar to an android activity. This section
briefs different stages of its life cycle.
Fragment Life Cycle
Fragment Life Cycle
• 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.
Fragment Life Cycle
• 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.
Use Fragments
This involves number of simple steps to create 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
• Basically fragments are divided as three stages as
shown below.
• 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.
Thank You!

You might also like