Week2.pptx

COIT20270 Application Development for Mobile Platforms Week 2: Activities, Fragments and Intents

Dr. R. Balsys, CQU, 2018.

Source: Beginning Android Programming with Android Studio, J.F. DiMarzio, 2016

Week 2 – Activities, Fragments and Intents

Objectives to:

Understand Android activities and fragments

Use progress dialogs

Linking activities using intents

Resolving intent filter collisions

Returning results from an intent

Passing data using intent objects

CQU - COIT20270 Application Development for Mobile Platforms

Understanding Activities

Activities extend the Activity base class

The Activity base class life cycle includes onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy() and onRestart() events

Figure 3-1 in DiMarzio text shows the life-cycle of an activity and the stages it goes through

An activity usually occupies the whole screen but can behave as a dialog by modifying the <Activity> element by adding android:theme=“@android:style/Theme.Dialog” in the AndroidManifest.xml file

CQU - COIT20270 Application Development for Mobile Platforms

Demonstrate Explain script functions

3

Adding Themes to an Activity

In Android 6 a new theme, Material, has been used for the default look and feel

Use android:Theme=“@android:style/Theme.Material” in the application tag of the android.manifest file to set this as the default theme for the app

CQU - COIT20270 Application Development for Mobile Platforms

Hiding the Activity Title and Displaying a Dialog Window

The activity title can be hidden by importing android.view.Window and using requestWindowFeature(Window,FEATURE_NO_TITLE) in the onCreate() method

To display a dialog you implement the onCreateDialog() method

onCreateDialog() calls showDialog()that uses on integer to select the actual dialog to display

The Hiding the Activity Title Try It Out shows you how to display a dialog window when you need to get the user to confirm an action

CQU - COIT20270 Application Development for Mobile Platforms

Displaying a Progress Dialog

When an action will take more than 10 seconds a progress dialog should be displayed

To create a progress dialog you create an instance of the ProgressDialog class and call its show() method

An example is in the Displaying a Dialog section of the DiMarzio text

You can also set properties such as a progress icon, title and style. You also can add Cancel and OK buttons to the dialog

A more sophisticated version is given example is in the Displaying a Progress Dialog section of the DiMarzio text

CQU - COIT20270 Application Development for Mobile Platforms

Point out the (1,0) and (0,1) pixels to get the point home

6

Linking Activities Using Intents

Android applications can have more than 1 activity

Navigation between activities is achieved using intents

You add the name of the new class in the AndroidManifest.xml file and give it a unique label

You supply an intent filter and set the category for the intent filter as android.intent.category.DEFAULT

The DiMarzio text examples is in the Linking Activities with Intents try it out.

CQU - COIT20270 Application Development for Mobile Platforms

7

Resolving Intent Filter Collision

If more than two <intent-filter> elements have the same name then when the application is run Android will present the user with a list of choices to pick from

The chosen item becomes the default choice

To clear the default choice you have to go to the Application Settings to clear the default choice

CQU - COIT20270 Application Development for Mobile Platforms

Returning Results from an Intent

The startActivity() does not return data

If you need to return data from an Activity use the startActivityForResult() method instead

The startActivityForResult() method has a second parameter that returns a resultCode that identifies the activity being called

To return the value you need send the data back via the setData() method

The setData() method calls a setResult() method to set the resultCode

In the calling activity you implement the onActivityResult() method to deal with the result

CQU - COIT20270 Application Development for Mobile Platforms

Passing Data Using an Intent Object

Data is passed to an activity using an Intent object

The putExtra() method puts name:value pairs into an Intent object

A Bundle object can also attach data to Intent objects using the putExtra() method

To obtain the data in the Intent object use the getIntent() method and call its getStringExtra() method to get the string values using the putExtra() method

CQU - COIT20270 Application Development for Mobile Platforms

Explain script functions

10

…Passing Data Using an Intent Object

To retrieve the Bundle object use the getExtras() method

Individual name:value pairs are retrieved using the appropriate method, eg getString()

The data can also be retrieved using the setData() method

CQU - COIT20270 Application Development for Mobile Platforms

Explain script functions

11

Week 2 – Mobilising Your Apps

Objectives, to:

Understand fragments

Adding fragments dynamically

Fragment lifestyle

Fragment interactions

Calling built-in applications using intents

Understand the intent object

Use intent filters

Adding categories

Displaying notifications

CQU - COIT20270 Application Development for Mobile Platforms

Fragment

Fragments are contained within an Activity

Many Fragments can be contained within a single Activity

Fragments contain views that differ from one another

Fragments are a versatile way of of creating user interfaces for Android applications

Fragments can be dynamically added or removed from Activities

CQU - COIT20270 Application Development for Mobile Platforms

…Fragments

The Java class for a Fragment extends Fragment

To draw the UI for a Fragment you over-ride the onCreateView() method that returns a View object

In onCreateView() a LayoutInflater object inflates the UI from an XML file

You add a fragment to an Activity with the <fragment> element

The <fragment> element uses unique id’s for the element using the android:id or android:tag attributes

CQU - COIT20270 Application Development for Mobile Platforms

Adding Fragments Dynamically

It is more useful to be able to add Fragments at run-time than by configuring an XML file during design

You use the FragmentManager class to add fragments

Use the FragmentTransactions class to add, remove or replace fragments

To ensure changes take place you call the commit() method on your FragmentTransaction object

CQU - COIT20270 Application Development for Mobile Platforms

Life Cycle of a Fragment

When a Fragment is being created it goes through onAttach(), onCreate(), onCreateView() and onActivityCreated() states

When visible it goes through onStart() and onResume() states

When it goes into the background it goes through onPause() and onStop() states

When the Fragment is destroyed it goes through onPause(), onStop(), onDestroyView(), onDestroy() and onDetatch() states

CQU - COIT20270 Application Development for Mobile Platforms

…Life Cycle of a Fragment

When an Activity goes into the background it is placed into the back stack allowing it to resume if the Back button is pressed

Fragments must be manually placed in the back stack by calling the addToBackStack() method

CQU - COIT20270 Application Development for Mobile Platforms

Interactions Between Fragments

Fragments need to communicate with one-another to be effective

The activity to which a fragment belongs can be determined using the getActivity() method

Fragment Views can be found using the findViewById() method

The example in the Interaction Between Fragments try it out of the DiMarzio text demonstrates fragment interaction

CQU - COIT20270 Application Development for Mobile Platforms

Calling Built-in Applications Using Fragments

Intents can be used to call other Android applications from within a fragment

Intents are paired with action and data intents

The action and data pairs describe the operation to be performed

You create an Intent object and then pass action and data arguments to the Intent

CQU - COIT20270 Application Development for Mobile Platforms

The Intent Object

You can call another activity by passing its action to the constructor of an Intent object

You can also create an Intent object by passing in an action constant and data

For some Intents the data type is not needed, the MIME type is set using setType()

Intent objects can also be set using a category which groups activities into logical units for filtering

CQU - COIT20270 Application Development for Mobile Platforms

Using Intent Filters

In order for an activity to invoke another activity requires the use of an <intent-filter> in the AndroidManifest.xml file

If multiple actions match the intent-filter then a dialog appears for the user to choose the correct one

A createChooser() method can be used to customise this dialog and prevent a crash when no match is found

CQU - COIT20270 Application Development for Mobile Platforms

Adding Categories

To add a category you must first define an <intent-filter> for the category,

Eg. <category android:name=“abc.net.au/justin” />

You must also add the category using the addCategory() method on an Intent instance

eg. Intent I = new (….);

i.addCategory(“abc.net.au/justin”);

CQU - COIT20270 Application Development for Mobile Platforms

Displaying Notifications

The NotificationManager class is used to display persistent messages in the navigation bar at the top of the device

To use notifications you first declare a new Intent that uses the NotificationView.class

You also create a PendingIntent instance initialised using the getActivity() method to set the PendingIntent’s context, request code, intent and flags

You then obtain an instance of the NotificationManager class and create an instance of the Notification class

CQU - COIT20270 Application Development for Mobile Platforms

…Displaying Notifications

You then use the setLatestEventInfo() method on the Notification instance to setup the notification and its response

You then call the notify() method on your NotificationManager instance to display the notification

CQU - COIT20270 Application Development for Mobile Platforms