您的位置:首页 > 编程语言 > C语言/C++

C++到底还能做什么?

2010-05-09 22:02 417 查看
An activity is an component that provides a window with which user can interact. The window typically fills the whole screen, or may float on top of other windows.

 

An application usually consists of multiple activities, one as the main activity and can start other activities based on the user actions.

 

The system preserves the activities in a stack("back stack") which abides to the "last in, first out" principle. see Tasks and Back Stack. Activity uses callback methods to respond to changes in state in its lifecycle.

 

Creating an Activity

 

you must create a subclass of Activity, you need to implement callback methods. The most important methods are:

 

onCreate

invoked when creating your activity. initialize and define the layout for the user interface.

 onPause

invoked when user is leaving your activity

Implementing a user interface
 

 

The user interface for an activity is provided by a hierarchy of views -- objects derived from the View class. Each view controls a particular rectangular space within the activity's window(i.e. Button)

 

Widgets are views that are visual elements, while Layouts are views derived from ViewGroup that provide a unique layout for its child views.

 

The most common way to define a layout using views is with an XML layout file.

Delcaring the activity in the manifest

 

You must declare your activity in the manifest file making it accessible to the system. The android:name is the only required attribute that specifies the class name of the activity. An activity uses intent-filter to declare how other application may activate it.

 

Starting an Activity

 

You can activate an activity by creating an intent that explicitly defines the activity you want to start:

 

 

Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);

 

 

If you want to activate an external activity to perform some actions for you, you can create the following intent:

 

 

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
startActivity(intent);

 

 

sometimes, you want to receive the result from the activity you start, you need to implement the callback method onActivityResult(), then you can get the result in an Intent in your onActivityResult method.

 

 

private void pickContact() {
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {
Cursor cursor = getContentResolver().query(data.getData()), new String[]{Contacts.DISPLAY_NAME}, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
String name = cursor.getString(columnIndex)
// do something here
}
}
}

 
 

Shutting Down an Activity

 

You can shut down an activity by calling its finish() method, or shut down an activity you previously started by calling finishActivity()

 

Managing the Activity Lifecycle

 

You can manage your activity lifecycle by implementing callback methods. An activity can exists in three states:

 

Resumed

in foreground of the screen, also referred to as "running"

Paused

another activity is on top of this one, but it is still visible because the top one is partially transparent or don't cover the entire screen. 

A paused activity is still alive but can be killed by the system in extremely low memory situations.

Stopped

The activity is completely obscured by another activity.

A stopped activity is still alive but no longer visible to user and can be killed by the sytem when more memory is needed elsewhere.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: