activity class

52
Romain Chiappinelli

Upload: luis-santos

Post on 18-Jul-2016

9 views

Category:

Documents


0 download

DESCRIPTION

Activity Class Description for Andriod

TRANSCRIPT

Page 1: Activity Class

Romain  Chiappinelli  

Page 2: Activity Class
Page 3: Activity Class
Page 4: Activity Class

The activity class The task backstack The activity lifecycle Starting activities Handling configuration changes

Page 5: Activity Class

Provides a visual interface for user interaction

Page 6: Activity Class

Each activity typically supports one focused thing a user can do, such as

Viewing an email message Showing a login screen

Page 7: Activity Class

Applications often comprise several activities

Page 8: Activity Class

Android supports navigation in several ways: Tasks The task backstack Suspending & resuming activities

Page 9: Activity Class

A Task is a set of related Activities These related activities don’t have to be part of the same application Most tasks start at the home screen

See: http://developer.android.com/guide/topics/ fundamentals/tasks-and-back-stack.html

Page 10: Activity Class

When an Activity is launched, it goes on top of the backstack When the Activity is destroyed, it is popped off the backstack

Page 11: Activity Class

Activity 3 Activity 2 Activity 1

Activity 1 Activity 2 Activity 1

Page 12: Activity Class

Activities are created, suspended, resumed & destroyed as necessary when an application executes

Page 13: Activity Class

Some of these actions depend on user behavior Some depend on Android

e.g., Android can kill activities when it needs their resources

Page 14: Activity Class

Resumed/Running - visible, user interacting Paused - visible, user not interacting, can be terminated* Stopped - not visible, can be terminated

Page 15: Activity Class

Android announces activity lifecycle state changes to activity by calling specific activity methods

Page 16: Activity Class

protected void onCreate (Bundle savedInstanceState) protected void onStart() protected void onResume() protected void onPause() protected void onRestart() protected void onStop() protected void onDestroy()

Page 17: Activity Class
Page 18: Activity Class
Page 19: Activity Class

Entire Lifetime

Page 20: Activity Class

Visible

Page 21: Activity Class

Visible & in Foreground

Page 22: Activity Class
Page 23: Activity Class

   

Page 24: Activity Class
Page 25: Activity Class
Page 26: Activity Class

Called when Activity is created Sets up Initial state

Call super.onCreate() Set the Activity’s content view Retain references to UI views as necessary Configure views as necessary

Page 27: Activity Class

MapLocation

Page 28: Activity Class

Called if the Activity has been stopped and is about to be started again Typical actions

Special processing needed only after having been stopped

Page 29: Activity Class

Activity is about to become visible Typical actions

Start when visible-only behaviors Loading persistent application state

Page 30: Activity Class

Activity is visible and about to start interacting with user Typical actions

Start foreground-only behaviors

Page 31: Activity Class

Focus about to switch to another Activity Typical actions

Shutdown foreground-only behaviors Save persistent state

Page 32: Activity Class

Activity is no longer visible to user may be restarted later

Typical actions Cache state

Note: may not be called if Android kills your application

Page 33: Activity Class

Activity is about to be destroyed Typical actions

Release Activity resources

Note: may not be called if Android kills your application

Page 34: Activity Class

MapLocation

Page 35: Activity Class

MapLocation

Page 36: Activity Class

Create an Intent object specifying the Activity to start

Page 37: Activity Class

Pass newly created Intent to methods, such as: startActivity() startActivityForResult()

Invokes a Callback method when the called Activity finishes to return a result

Page 38: Activity Class

MapLocation

Page 39: Activity Class

Similar to MapLocation, but gets address from contacts database

Page 40: Activity Class
Page 41: Activity Class

MapLocationFromContacts

Page 42: Activity Class

Started Activity can set its result by calling Activity.setResult()

public final void setResult (int resultCode) public final void setResult (int resultCode,

Intent data)

Page 43: Activity Class

resultCode (an int) RESULT_CANCELED RESULT_OK RESULT_FIRST_USER

Custom resultCodes can be added

Page 44: Activity Class

MapLocationFromContacts

Page 45: Activity Class

MapLocationFromContacts

Page 46: Activity Class

Keyboard, orientation, locale, etc.

Device configuration can change at runtime On configuration changes, Android usually kills the current Activity & then restarts it

Page 47: Activity Class

Activity restarting should be fast If necessary you can:

Retain an Object containing important state information during a configuration change Manually handle the configuration change

Page 48: Activity Class

Hard to recompute data can be cached to speed up handling of configuration changes Override onRetainNonConfigurationInstance() to build & return configuration Object

Will be called between onStop() and onDestroy()

Page 49: Activity Class

Call getLastNonConfigurationInstance() during onCreate() to recover retained Object Note: These methods have been deprecated in favor of methods in the Fragment class (discussed in later classes)

Page 50: Activity Class

Can prevent system from restarting Activity Declare the configuration changes your Activity handles in AndroidManifest.xml file, e.g., <activity android:name=".MyActivity“� android:configChanges=�

"orientation|screensize|keyboardHidden”…>

Page 51: Activity Class

When configuration changes, Activity’ s onConfigurationChanged() method is called Passed a Configuration object specifying the new device configuration

Page 52: Activity Class

The Intent Class