Tải bản đầy đủ (.pptx) (70 trang)

Module 4 user interfaces activity

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (550.56 KB, 70 trang )

Android Basic Course
© 2012 University of Science – HCM City
.
M.Sc. Bui Tan Loc

Department of Software Engineering,
Faculty of Information Technology,
University of Science – Ho Chi Minh City, Viet Nam
Module 4: User Interfaces
Section 2: Activity
Android Basic Course
© 2012 University of Science – HCM City
.
High-level diagram class of the Android View API
2
View
ViewGroup
Activity


… …
Android built-in layout classes
Android built-in view container classes
Android built-in single view classes
Android Basic Course
© 2012 University of Science – HCM City
.
Objectives

After completing this section, you will able to:


Create an activity with loading layout form xml-based layout file.

Manage the activity lifecycle.

Start or shut down an activity.

Create resources of many types of menus.

Handle menu item selections.

Use event handlers.

Use event listeners.
3
Android Basic Course
© 2012 University of Science – HCM City
.
Contents

Activity overview

Activity skeleton code

Activity lifecycle

Different types of menus

Creating a menu resource

Common Activity’s methods for working with menus


Common Menu’s methods for working with submenus

Common MenuItem’s methods for working with menu
items

User Input events

Using event handlers

Using event listeners
4
Android Basic Course
© 2012 University of Science – HCM City
.
Activity Overview

Activity (user interaction): UI component typically
corresponding to a screen.

Ex: An email application might have one activity that shows a list of
new emails, another activity to compose an email, and another
activity for reading emails.

Activities themselves are made up of subcomponents
called views.

Activities using Intent Filters and Permissions determine
how they interact with each other and with other
applications.


Activities must be declared in the Android manifest file.
5
Android Basic Course
© 2012 University of Science – HCM City
.
Designing layout by using xml layout text editor
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=" /> android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
6
Android Basic Course
© 2012 University of Science – HCM City
.
Designing layout by using xml layout GUI editor
7
Android Basic Course
© 2012 University of Science – HCM City
.
Loading layout from xml-based layout file
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
8
Android Basic Course
© 2012 University of Science – HCM City
.
Creating layout classes by using source code –
xml layout built-in source code
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
LinearLayout linear;
linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
linear.addView(tv);
setContentView(linear);
}
}

9
Android Basic Course
© 2012 University of Science – HCM City
.
High-level diagram of activity

Views and other Android
components use strings,
colors, styles, and graphics,
which are compiled into a
binary form and made
available to applications as
resources.

The automatically generated
R.java provides a reference to
individual resources and is the
bridge between binary
references and the source
code of an Android
application.
10
Android Basic Course
© 2012 University of Science – HCM City
.
Activity skeleton code
public class ExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// The activity is being created.
}
@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
}
@Override
protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
}

11
Android Basic Course
© 2012 University of Science – HCM City
.
Activity skeleton code

@Override
protected void onPause() {
super.onPause();
// Another activity is taking focus (this activity is about to be "paused").
}
@Override
protected void onStop() {
super.onStop();
// The activity is no longer visible (it is now "stopped")
}
@Override

protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
}
}
12
Android Basic Course
© 2012 University of Science – HCM City
.
The activity lifecycle
13
Android Basic Course
© 2012 University of Science – HCM City
.
Android Activity main lifecycle methods and their purpose
Method Purpose
onCreate() Called when the Activity is created. Setup is done here. Also provided is
access to any previously stored state in the form of a Bundle.
onRestart() Called if the Activity is being restarted, if it’s still in the stack, rather
than starting new.
onStart() Called when the Activity is becoming visible on the screen to the user.
onResume() Called when the Activity starts interacting with the user. (This method
is always called, whether starting or restarting.)
onPause() Called when the Activity is pausing or reclaiming CPU and other
resources. This method is where you should save state information so
that when an Activity is restarted, it can start from the same state it
was in when it quit.
onStop() Called to stop the Activity and transition it to a nonvisible phase and
subsequent lifecycle events.
onDestroy() Called when an Activity is being completely removed from system

memory. This method is called either because onFinish() is directly
invoked or the system decides to stop the Activity to free up resources.
14
Android Basic Course
© 2012 University of Science – HCM City
.
Another activity lifecycle diagram

In the foreground phase, the Activity is viewable on the
screen and is on top of everything else.

In the visible phase, the Activity is on the screen, but it
might not be on top and interacting with the user.

The entire lifecycle phase refers to the methods that
might be called when the application isn’t on the screen,
before it’s created, and after it’s gone.
15
Android Basic Course
© 2012 University of Science – HCM City
.
The onSaveInstanceState() function

Whenever an activity is about to be killed, the
onSaveInstanceState() function is called. Override this
to save relevant information that should be
retained.When the activity is then recreated, the
onRestoreInstanceState() is called. Override this
function (or onCreate()) to retrieve the saved
information.


The onSaveInstanceState() function is distinct from
onPause(). For example, if another component is launched
in front of the activity, the onPause() function is called.
Later, if the activity is still paused when the OS needs
to reclaim resources, it calls onSaveInstanceState()
before killing the activity.
16
Android Basic Course
© 2012 University of Science – HCM City
.
Activity Life Cycle Sample

Open Child Activity
> onCreate(null) -> onStart() -> onResume() -> [Open Child Activity]
> onSaveInstanceState() -> onPause() -> onStop() -> [Close Child Activity]
> onRestart() -> onStart() -> onResume()

Transparent View
> onCreate(null) -> onStart() -> onResume() -> [Open Transparent View]
> onSaveInstanceState() -> onPause() -> [Close Transparent View]
> onResume()

Turn Display
> onCreate(null) -> onStart() -> onResume() -> [Turn Display]
> onSaveInstanceState() -> onPause() -> onStop() -> onDestroy() ->
[Recreate]
> onCreate() -> onStart() -> onRestorInstanceState() -> onResume()
17
Android Basic Course

© 2012 University of Science – HCM City
.
Activity Life Cycle Sample

Home
> onCreate(null) -> onStart() -> onResume() -> [Home Button]
> onSaveInstanceState() -> onPause() -> onStop() -> [Start App]
> onRestart() -> onStart() -> onResume()

Phone call interrupt
> onCreate(null) -> onStart() -> onResume() -> [Phone Call]
> onSaveInstanceState() -> onPause() -> onStop() -> [Hang Up or press
Back]
> onRestart() -> onStart() -> onResume()
18
Android Basic Course
© 2012 University of Science – HCM City
.
Starting an activity for passing no values

Starting an activity for passing no values:
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
19
Android Basic Course
© 2012 University of Science – HCM City
.
Starting an activity for passing values

Starting an activity for passing values by putting values

into intent object:
//SignInActivity
public static final String NAME_KEY = “name”;
public static final String PASS_KEY = “pass”;

Intent intent = new Intent(this, OrderActivity.class);
intent.putExtra(this.NAME_KEY, nameValue);
intent.putExtra(this.PASS_KEY, passValue);
startActivity(intent);
20
Android Basic Course
© 2012 University of Science – HCM City
.
Catching passed values
//OrderActivity

public void OnCreate(…){

Bundle extras = getIntent().getExtras();
String usrName = extras.getString(SignInActivity.NAME_KEY);
String passWord = extras.getString(SignInActivity.PASS_KEY);

}
21
Android Basic Course
© 2012 University of Science – HCM City
.
Starting an activity for results
//NoteList Activity
public static final int ACTIVITY_CREATE = 0;

public static final int ACTIVITY_EDIT = 1;

//Open creating note screen
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);

//Open editing note screen
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_EDIT);

22
Android Basic Course
© 2012 University of Science – HCM City
.
Catching returned results
//NoteEdit Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bundle extras = data.getExtras();
switch(requestCode) {
case ACTIVITY_CREATE:
if ( resultCode = RESULT_OK){
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mDbHelper.createNote(title, body);
fillData();
}
break;
case ACTIVITY_EDIT:

Long mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (mRowId != null) {
String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
mDbHelper.updateNote(mRowId, editTitle, editBody);
}
fillData();
break;
}
}
23
Android Basic Course
© 2012 University of Science – HCM City
.
Shutting Down an Activity

Returning no results:
finish();

Returning result cancelled:
setResult(RESULT_CANCELLED, null);
finish();

Returning result ok:
Intent mIntent = new Intent();
mIntent.putExtras(…);
setResult(RESULT_OK, mIntent);
finish();
24
Android Basic Course

© 2012 University of Science – HCM City
.
Shutting Down an Activity

You can also shut down a separate activity that you
previously started by calling finishActivity(). This
method is used for shutting down an activity being in the
foreground.
startActivityForResult(intent, requestCode);
try {
Thread.sleep(3500);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finishActivity(requestCode);
25

×