Android Activity Study

The defination of 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, send an email, or view a group.

The expend define:

The UI system wil gather the info from the user, just as open or close the system, load one database, connect to internet. All the info will pass to application. And the app will divide them to different class to deal with.In activity, it will only deal with the open adn close actions. it won't deal with the input issues.The Window Class embedded in Activity will deal with it and pass it to View.The first entre to activity will use onCreate method, but the second time, it will call onStart to deal with the problems you left last time. 

When you entre one Activity, it will call onCreate, onStart and on Resume. When you left, it will call onPause and onStop. the order will be the same everytime. 

There is one ViewRoot. It is not the View or the subclass of View.It responses the connection between WindowManage and WindowManagerService. The WindowManagerService manage all the system windows.

More than one Activities.

When the current activity jump to the other activity. the current activity will run onPause(). Then run the second activity onCreate(). When the activity comes back, the second activity will run onPause(), then run the first one's onCreate, onStart, onResume. Notice, the second activity now will run onStop and onDestory now!


Why should we pause the first Activity and then run the Second Activity?

The android should make sure the first Activity will not interfere the second activity. When you listent the music and the phone is coming now, the android should pause the music and to take the phone.

When the view changed from horizantal to vertical, the activity will call onPause, onStop, and onDestory. the activity will start new activity to change the direction of view. So the app will call onCreate, onStart, and onResume. 


Initiate one Activity

There are two ways to start one activity:

1.Intent intent = new Intent(this, xxx.class);

   startActivity(intent);

2.Intent intent = new Intent();

   ComponentName component = new ComponentName(this, xxx.class);

   intent.setComponent(component);

   startActivity(intent);

The app can also start one Activity from other's app.

In the manifest modify the settings of the activity. add:


<intent-filter>

<action android:name="xxx">

<category android:name="android.intent.category.DEFAULT">

</intent-filter>


and in the buttom to override the method:


Intent  intent = new Intent();

intent.setAction("xxx");

startActivity(intent);


Share the data between the activities

From the send end, the app should put the data in Bundle and then use intent to send the data while switch the actiivty.

Bundle bundle = new Bundle();

bundle.putParceble("name",value);

intent.putExtra(bundle);


The app can never trans the data larger than 0.25 MB. The binder cannot handle it.


Task and its start mode

A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack(the "back stack"), in the order in which each activity is opened.


There are four mode of starting :
Standard mode(default):
The created activity will push into stack. when the app exit, the activity will be poped.
SingleTop:
If the activity is on the top of the stack, the activity will not be recreated again.If not, the activity will be onCreate() immiediately.
SingleTask:
If the stack has the activity, it will erase all the acitivities above it, and then call the newInstance() method to reuse the activity to let the  activity be the top of the stack or create a new one.
SingleInstance mode:
The activity wil be  created in one new stack. When you press back buttom, the activities in the old stack will be finished until the last one is over, the activity in the new stack will be show up。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章