activity的开发指南

全部都是阅读官方阅读之后的理解,官方指南在此:http://developer.android.com/guide/components/activities.html

 

Activity的注册:

这是必须做的,但是分两种,一种是如果需要响应他人的广播,也就是说如果改activity有被其他应用调用的可能的话,那就就必须在注册那里加上

<intent-filter>

另外一种是,只需要被自己的应用调用的话,就不用<intent-filter>了。

 

被启动的Activitiy的分类:

分为两种,一直是被启动的Activity与当前Activity并没有结果上的关系,那么直接使用startActivity(intent)即可;

另外一种是需要从被启动的Activity获取结果,则使用startActivityForResult(intent, PICK_CONTACT_REQUEST);

详细的代码见官方指南。

 

Activitiy的生命周期

这个就不多说了,入门的教程基本都有,列下基本的周期选项。

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").
    }
    @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.
    }
}

注意,官方并不建议手动进行ACtivity的管理,只要在相应的周期里写好代码,其他的由系统来管理即可。

 

Taken together, these methods define the entire lifecycle of an activity. By implementing these methods, you can monitor three nested loops in the activity lifecycle:

  • The entire lifetime of an activity happens between the call to onCreate() and the call to onDestroy(). Your activity should perform setup of "global" state (such as defining layout) in onCreate(), and release all remaining resources in onDestroy(). For example, if your activity has a thread running in the background to download data from the network, it might create that thread in onCreate() and then stop the thread inonDestroy().
  • The visible lifetime of an activity happens between the call to onStart() and the call to onStop(). During this time, the user can see the activity on-screen and interact with it. For example, onStop() is called when a new activity starts and this one is no longer visible. Between these two methods, you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart()to monitor changes that impact your UI, and unregister it in onStop() when the user can no longer see what you are displaying. The system might call onStart() and onStop() multiple times during the entire lifetime of the activity, as the activity alternates between being visible and hidden to the user.

  • The foreground lifetime of an activity happens between the call to onResume() and the call to onPause(). During this time, the activity is in front of all other activities on screen and has user input focus. An activity can frequently transition in and out of the foreground—for example, onPause() is called when the device goes to sleep or when a dialog appears. Because this state can transition often, the code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait.

 

关于状态的保存

在各种Activity跳转的时候,系统会通过onSaveInstanceState()、onRestoreInstanceState()进行某些控件状态的保存,这样你辛辛苦苦写完的微博不会因为一个突然来电而消失。

在系统中,系统已经写好了一些方法,这两个方法我们可以不进行重写,我们所需要做的,就是

 The only work required by you is to provide a unique ID (with the android:id attribute) for each widget you want to save its state. If a widget does not have an ID, then the system cannot save its state.

就是给控件们一人一个ID。

 

onPause()与onStop()的联系

首先,两者的区别是,一个是被Activity挡在前面(但是并没有被完全遮住)的状态,这时候是onPause

另外一个onStop,只有当前Activity被完全遮住的时候,才会进入该状态。

当两个Activity进行相互切换的时候,他们的生命周期是这样的:

The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other. Here's the order of operations that occur when Activity A starts Acivity B:

  1. Activity A's onPause() method executes.
  2. Activity B's onCreate()onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
  3. Then, if Activity A is no longer visible on screen, its onStop() method executes.

This predictable sequence of lifecycle callbacks allows you to manage the transition of information from one activity to another. For example, if you must write to a database when the first activity stops so that the following activity can read it, then you should write to the database during onPause() instead of duringonStop().

 

最后值得一提的是,系统能杀掉的那些进程的状态只有三个,分别是 (onPause()onStop(), and onDestroy())。

由于onPause()是最先可杀掉的状态,所以如果要进行数据的保存等,记得在onPause()处理好这些关系。

posted @ 2013-07-22 12:49  yutoulck  阅读(329)  评论(0编辑  收藏  举报