Top Banner
2/4/2016 1 CHAPTER 3 Activities and Intents Chapter objectives: Explore an Activity’s Lifecycle Learn about saving and restoring an Activity Understand Intents and how they are used with multiple Activities Become familiar with passing data between Activities Implement applications that require basic animation Activity transitions Study Scene transitions
25

Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

Mar 22, 2018

Download

Documents

lamdien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

1

CHAPTER 3

Activities and Intents

Chapter objectives:

• Explore an Activity’s Lifecycle

• Learn about saving and restoring an Activity

• Understand Intents and how they are used with multiple Activities

• Become familiar with passing data between Activities

• Implement applications that require basic animation Activity transitions

• Study Scene transitions

Page 2: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

2

3.1 Activity Lifecycle

• All applications are composed of at least one Activity class• In most cases, applications will require the use of several

activities• Activities in an application are often loosely connected to

each other. Information can be passed from one activity to another, but they remain distinct and separate in every other way.

• Every application has one activity class that serves as the main activity

• Each time a new activity starts, the previous activity is paused and its status is preserved by the Android system.

3.1 Activity Lifecycle

Figure 03.01: A Chess Game App Showing Two Activities: Game Play and Chatting

Page 3: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

3

• Activities must be declared in the AndroidManifest.xml file in order to be accessible to the system

• Activities are defined using the <activity> tag.

• An <activity> must be added as a child to the <application> element

• The activities in an application are implemented as a subclass of Activity

• The Activity class is an important part of every application’s overall lifecycle

• When an application is first loaded, its main activity, specified within the AndroidManifest, is immediately created

• Once the main activity is started, it is given a window in which to draw its layout

• Its layout is its associated user interface screen.

Page 4: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

4

• The Activities in an application are managed by an Activity stack

• New activities are pushed onto the top of the stack and become the running activity

• The Activity class defines the following seven callback methods, beginning with the creation of the Activity and ending with its destruction

• onCreate()• onStart()• onResume()• onPause()• onStop()• onRestart()• onDestroy()

Page 5: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

5

• If a paused or stopped activity requires destruction due to a system need for more memory, the activity will need to be recreated again. This will be done by calling onCreate().

• onStart() is always to be called after onRestart()

• onStop() is called when the activity is no longer visible to the user

• onDestroy() performs final cleanup prior to an activity’s destruction

Page 6: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

6

3.2 Starting, Saving, and Restoring an Activity

• When an activity is paused or stopped, the state of the activity is retained

• When the system destroys an activity in order to recover memory, the memory for that activity object is also destroyed

• A Bundle is a container for the activity state information that can be saved

• The following is an incomplete list of these methods:

• putChar()

• putString()

• putBoolean()

• putByte()

• putFloat()

• putLong()

• putShort()

• putParcelable()

Page 7: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

7

• Recover the saved state from the Bundle that the system passes to the activity

• onCreate() and onRestoreInstanceState() callback methods receive the same Bundle object that contains the instance state information

• Check whether the activity’s state (stored in the Bundle object) is null before you attempt to read it

• If it is null, then the system is creating a new instance of the Activity class, instead of restoring a previous one that was destroyed

• During runtime, the user may alter the screen orientation.

• The system will automatically recreate the currently running activity by calling onDestroy(), and then immediately calling onCreate().

• Application adapts to new configurations by automatically reloading the application with alternative resources, such as adaptive layouts for different screen size and orientation

Page 8: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

8

Lab Example 3-1: Activity Lifecycle Exploration Application

Figure 03.03: Activity lifecycle experiment application

Lab Example 3-1: Activity Lifecycle Exploration Application

Figure 03.04: The project structure of Activity Lifecycle Explore application

Page 9: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

9

Lab Example 3-1: Activity Lifecycle Exploration Application

Figure 03.05: Common Unicodes

Lab Example 3-1: Activity Lifecycle Exploration Application

Figure 03.06: The layout for Activity Lifecycle Explorer app is blank with aRelativeLayout root

Page 10: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

10

3.3 Multiple Activities and the Intent Class

• Applications that are built with multiple activities need to utilize the Intent class

• This class provides the framework for the navigation from one screen to another.

• An Intent object is a message from one component to another component, either within the application or outside the application

• Intents are designed to communicate messages between three application core components

• Activities

• Broadcast receivers

• Service:

Page 11: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

11

3.3.1 Explicit Intents

• Explicit Intents use a specific name when starting a component

• This name will be the full Java class name of the activity or service

• The most common use of an explicit Intent is the launching of a target component with a known name within the currently running application.

3.3.2 Implicit Intents

• Unlike an explicit Intent, an implicit Intent does not name a specific component

• it declares a general action to perform, which allows a component from another app to handle it

• When an implicit Intent is created, the system locates the appropriate target component by comparing the contents of the Intent to an Intent filter

• Intent filters are declared in the manifest file of other apps located on a given device

• When the Intent has found a match with the Intent filter, the system starts that component and delivers it to the Intent object.

Page 12: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

12

3.4 Handling Keyboard Visibility

• Android shows or hides the soft keyboard when input focus moves into or out of an editable text field

• The system also makes decisions about how your UI and the text field appear above the keyboard

• It is possible to specify how you want your layout to appear when the keyboard is visible

• Android gives focus to the first EditText element in the layout launched by a running activity

• It is also possible to request the focus of a View programmatically

• requestFocus can be called to give focus to a specific View or to one of its descendants

• This can be useful in cases when you want to ensure that the keyboard is visible.

Page 13: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

13

Lab Example 3-2: Navigating Multiple Screens – Paint Calculation App

Figure 03.07: The Paint Job Calculator uses two screens: input and help

Lab Example 3-2: Navigating Multiple Screens – Paint Calculation App

Figure 03.08: Project structure for the Paint Calculator application

Page 14: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

14

Lab Example 3-2: Navigating Multiple Screens – Paint Calculation App

Figure 03.09: The layout structure for input_layout.xml

Lab Example 3-2: Navigating Multiple Screens – Paint Calculation App

Figure 03.10: The layout design for help_layout.xml

Page 15: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

15

3.5 Passing Data

• The Android framework provides a simple and flexible approach to working with multiple activities

• Android also offers an efficient model for passing information between various activities.

• Data can be passed as a message object to an activity implemented within the application or an activity outside of the applications

• When an Intent object is constructed, its action is specified• This represents the action we want the Intent to trigger• This action will also send data across process boundaries

• It is possible to add extended data to a given Intent

• This is done using the putExtra() method

• putExtra() requires two parameters: a name for the data and a String data value

• The data name must include a package prefix

Page 16: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

16

Lab Example 3-3: Automotive Calculator App

Figure 03.11: Auto Purchase application

Lab Example 3-3: Automotive Calculator App

Figure 03.12: Project structure for the Auto Purchase application

Page 17: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

17

Lab Example 3-3: Automotive Calculator App

Figure 03.13: Graphic layout design for purchase_layout.xml

Lab Example 3-3: Automotive Calculator App

Figure 03.14: Layout structure for the loansummary_layout.xml

Page 18: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

18

Lab Example 3-4: Flip Cards with Animated Activity Transitions

Figure 03.15: The front and back of a flip card

Lab Example 3-4: Flip Cards with Animated Activity Transitions

Figure 03.16: Project structure of the Flip Card application

Page 19: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

19

Lab Example 3-4: Flip Cards with Animated Activity Transitions

Figure 03.17: activity_question.xml represents the front side of the card

Lab Example 3-4: Flip Cards with Animated Activity Transitions

Figure 03.18: activity_answer.xml represents the back side of the flashcard

Page 20: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

20

Lab Example 3-4: Flip Cards with Animated Activity Transitions

Figure 03.19: A question exits the screen by moving to the right

Lab Example 3-4: Flip Cards with Animated Activity Transitions

Figure 03.20: An answer image enters the screen from the left

Page 21: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

21

3.6 Basic Transitions Between Activities

• The quality of an application can depend on several characteristics, such as content, usability, and design features

• Enhancements made to interaction design, through the use of animation, can make a fundamental difference in the usability of an application

• Interaction plays a large role

• User interfaces are not static designs, but rather engaging and dynamic design patterns

• Custom transition animations are resources that can be built by methods in XML code

• The method overridePendingTransition() allows custom-built transitions to be entering and exiting activities

• overridePendingTransition() requires two arguments: an enter animation implementation and an exit animation implementation

Page 22: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

22

3.7 Scene Transitions

• KitKat, Android 4.4, has a transitions framework that supports the definition of scene transitions

• Scenes are used specifically for transition animations.

• Scenes can be created as a View hierarchy, much like a layout

• A scene contains values of various properties in the View hierarchy

• As scenes enter or exit a View hierarchy, they will be animated based on these properties

Lab Example 3-5: Pieces of Painting: The Painting Scene App

Figure 03.21: Animation will occur with the gradual appearance of the painting

Page 23: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

23

Lab Example 3-5: Pieces of Painting: The Painting Scene App

Figure 03.22: Animated Transition between Scenes

Lab Example 3-5: Pieces of Painting: The Painting Scene App

Figure 03.23: Project structure for the Paint Scene application

Page 24: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

24

Lab Example 3-5: Pieces of Painting: The Painting Scene App

Figure 03.24: The graphic design of the layout activity_my.xml

Lab Example 3-5: Pieces of Painting: The Painting Scene App

Figure 03.25: The visual design of the layout scene01.xml

Page 25: Activities and Intents - IPFWlin/CPET565/2016S/1-Lecture/CH... · Activities and Intents ... A Chess Game App Showing Two Activities: ... •This represents the action we want the

2/4/2016

25

Lab Example 3-5: Pieces of Painting: The Painting Scene App

Figure 03.26: The design for scene02.xml