2.Activity的概述
An Activity
is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.
An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes. (The back stack is discussed more in the Tasks and Back Stack document.)
When an activity is stopped because a new activity starts, it is notified of this change in state through the activity's lifecycle callback methods. There are several callback methods that an activity might receive, due to a change in its state—whether the system is creating it, stopping it, resuming it, or destroying it—and each callback provides you the opportunity to perform specific work that's appropriate to that state change. For instance, when stopped, your activity should release any large objects, such as network or database connections. When the activity resumes, you can reacquire the necessary resources and resume actions that were interrupted. These state transitions are all part of the activity lifecycle.
Activity提供了一个用户交互的窗口。一般的,首先需要创建Activity的一个子类作为 "main" activity----extends Activity,一个它需要设置一个用户接口 ----setContentView(R.layout.main),用户接口View与用户互动,对用户的操作产生响应,比如一个Button View可以对用户点击做出相对应的跳转或刷新操作。R.layout.main代表一个xml文件(res/layout/main.xml),可以编辑此文件添加各种系统已经实现的简单常用view控件如TextView,EditText,Button等。Application启动后开始运行“main”Activity,Activity可以根据不同的运行时机进行相应的工作处理和运行状态切换的数据保存和恢复。Activity生命周期有:onCreate、onStart、onResume、onPause、onStop、onDestroy
注:所有自己写的Activity必须在AndroidManifest.xml声明,这样系统才可以访问此Activity。intent-filter可以设置application启动时开始运行的第一个Activity。
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
<activity
android:name=".DemoAndroidActivity"
android:label="@string/app_name" android:theme="@android:style/Theme.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
publicclassExampleActivityextendsActivity{
@Override
publicvoidonCreate
(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// The activity is being created.开始创建
}
@Override
protectedvoidonStart()
{
super.onStart();
// The activity is about to become visible.开始,变得可见
}
@Override
protectedvoidonResume()
{
super.onResume();
// The activity has become visible (it is now "resumed").继续-- At this point the activity is at the top of the activity stack, with user input going to it.
}
@Override
protectedvoidonPause()
{
super.onPause();
// Another activity is taking focus (this activity is about to be "paused").暂停--Called when the system is about to start resuming another activity.
}
@Override
protectedvoidonStop()
{
super.onStop();
// The activity is no longer visible (it is now "stopped")开始停止,变得不可见
}
@Override
protectedvoidonDestroy()
{
super.onDestroy();
// The activity is about to be destroyed.开始销毁
}
}
// 应用启动“main”Activity运行
07-20 04:47:09.026: I/MainActivity(671): MainActivity-->onCreate
07-20 04:47:09.096: I/MainActivity(671): MainActivity-->onStart
07-20 04:47:09.106: I/MainActivity(671): MainActivity-->onResume
// 跳转到另一个Activity时
07-20 04:47:36.346: I/MainActivity(671): MainActivity-->onPause
07-20 04:47:36.535: I/MainActivity(671): SecondAndroidActivity-->onCreate
07-20 04:47:36.545: I/MainActivity(671): SecondAndroidActivity-->onStart
07-20 04:47:36.545: I/MainActivity(671): SecondAndroidActivity-->onResume
07-20 04:47:37.336: I/MainActivity(671): MainActivity-->onSaveInstanceState
07-20 04:47:37.336: I/MainActivity(671): MainActivity-->onStop
// 返回到“main”Activity
07-20 04:47:58.026: I/MainActivity(671): SecondAndroidActivity-->onPause
07-20 04:47:58.056: I/MainActivity(671): MainActivity-->onRestart
07-20 04:47:58.056: I/MainActivity(671): MainActivity-->onStart
07-20 04:47:58.066: I/MainActivity(671): MainActivity-->onResume
07-20 04:47:58.486: I/MainActivity(671): SecondAndroidActivity-->onStop
07-20 04:47:58.486: I/MainActivity(671): SecondAndroidActivity-->onDestroy
注意:保存的状态信息onSaveInstanceState方法写保存内容,在oncreate恢复:
edtxt = (EditText) findViewById(R.id.edtxt);
if(null!=savedInstanceState&&savedInstanceState.containsKey(CONTENT))
{
edtxt.setText(savedInstanceState.getString(CONTENT));
}
posted on 2012-07-20 12:54 SuperbookKing 阅读(172) 评论(0) 收藏 举报