Você está na página 1de 24

1

Mobile Computing
Activities

Lecture#05
2

Lecture Contents
vApplication Architecture
vApplication Types
vActivities
vServices
v
3

Application Architecture
vActivity Manager
ØControls Activity Life Cycle
ØControls Activity Stack
vViews
ØUI elements
ØComponents to be displayed on Activities
ØViewGroup
vNotification Manager
vContent Providers
vResource Manager
vAndroid Libraries (API)
4

Application Types
vForeground Applications
(Games, Interactive Applications etc)
v Background Applications
(Anti-virus, Screening applications, SMS Auto-responders)
vIntermittent Application
(Media Player, Download Manager etc)
vDesktop Widget
(Clock, Temperature, Converter Apps)
v
5

Phone Development
vHardware Imposed Design Consideration
vBe Efficient
vExpect Limited Capacity
vSmall Screen Size
vLow Speed, High Latency

6

Activity, Manifest & Resources


Relationship
7

Activity
vSingle focusable object (User interaction)
vUse setContentView() to set UI elements
vMostly full screen (can be a floating window as well
or even embedded inside other activity)
vActivity can be start from other activity:
üstartActivity()
üstartActivityForResult()
vManaged by an activity stack
vNew activity is placed on the top of the stack and
becomes the running activity (previous activity
always remains below it in the stack, and will not
come to the foreground again until the new activity
exits)
8

Base Activity Class


vBase Activity class (from API) represents an
empty screen without any functionality
vTo make it useful, one should extend it and add
UI (Views/ViewGroups) elements over it
vAll activities should be listed in manifest
vAn activity (in manifest) can describe intent-
filter to define which intents it will listen to
vAction tag within intent-filter tag defines how to
respond to an intent
9

Activity Basics …..


 <activity android:label="@string/label” android:name=".MyActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity>
10

Activity Creation
11

Regular Activity Floating Activity


12

Activity States
vActive/Running
a.Top of activity stack
b.Activity in the foreground of the screen (Visible)
c.In Focus
d.May be receiving input OR showing results
e.Android will put all efforts to keep it alive (killing
other processes/activities)
vVisible but Lost Focus
a.It is still visible (that is, a new non-full-sized or
transparent activity has focus on top of your
activity)
b.It is paused (Not in focus)
c.A paused activity is completely alive (it maintains all
state and member information and remains
attached to the window manager)

13

Activity States
vNot Visible but Alive
a.If an activity is completely obscured by another
activity, it is stopped
b.Still retains all state and member information,
c.It is no longer visible to the user
vStopped
a.System can drop the activity from memory by
either asking it to finish, or simply killing its
process
b.When displayed again to the user, must be
completely restarted and restored to its
previous state

14 Activity Life Cycle
15

Activity Life Cycle


public class Activity extends ApplicationContext {

protected void onCreate(Bundle savedInstanceState);


 When activity is first created (just once), Initialization tasks,
No visibility yet

protected void onStart();


After onCreate() or after onRestart(), activity being displayed to user,
followed by onResume()

protected void onRestart();


called after onStop() when activity is being re-displayed to user, will be
followed by onStart() and onResume()

protected void onResume();


Called after onRestoreInstanceState(Bundle), onRestart(), or onPause()
, for your activity to start interacting with the user. This is a good place
to begin animations, open exclusive-access devices (such as the
camera), etc.
16

Activity Life Cycle


 protected void onPause();
 Called when an activity is going into the background, but has not (yet)
been killed.

 protected void onStop();


 Called when activity is no longer visible to the user. Can be followed by
either onRestart(), onDestroy(), or nothing, depending on situation,
It may never be called, in low memory situations

 protected void onDestroy();


 Perform any final cleanup before an activity is destroyed. (as a result of
finish() call, or because the system is temporarily destroying this
instance of the activity to save space). One can distinguish between
these two scenarios with the isFinishing() method.
 }


17

Monitoring Activity States


18

Activity Life Times


vFull Lifetime
vVisible Lifetime
vActive Lifetime
v
19

Activity Types
vActivity
vListActivity
vMapActivity
vExpandableListActivity
vTabActivity
20

Launch a Screen
Intent activityStart = new Intent(currentActivity,

intendedActivity);
 startActivity(activityStart);

 Note: do not forget to put an entry for


intendedActivity in manifest or your application
will crash on startActivity() call


21

Launch a Screen for Result


Intent activityStart = new Intent(currentActivity,

intendedActivity);
 startActivityForResult(activityStart);
22

Launch a Screen for Result


How to return a result:


Bundle stats = new Bundle();


stats.putString(“department",“Finance"");

stats.putString(“position”, “Manager");

stats.putString(“name", “Asif Ali");

setResult(0, Activity.RESULT_OK, stats);


finish();
23

Launch a Screen for Result


How to process the result:

public void onActivityResult(int requestCode, int resultCode,


Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
Bundle extras = data.getExtras();

String dept= extras.getString(“department”);

String pos= extras.getString(“position”);

String name= extras.getString(“name”);

Do something with obtained data

}

}

24

Launch a Website
Intent browserIntent = new
Intent("android.intent.action.VIEW",
Uri.parse("http://www.google.com"));
startActivity(browserIntent);

Você também pode gostar