Activity的生命周期
當一個Activity被載入創建的時候經歷一下三個步驟
onCreate
onStart
onResume
當一個界面對用戶可見,可是不能進行相關操作時,這個界面就處在 onPause的狀態
當一個界面處在對用戶全然不可見的狀態,該界面就處於onStop的狀態 onPause -->onStop
onCreate()<---->onDestroy()
onStart()<----->onStop()
onResume()<----->onPause()
onRestart()界面處於onPause狀態,又又一次獲得焦點時調用該方法
Activity的生命周期
1.完整的生命周期( entire lifetime ):
onCreate()-->onStart()-->onResume()-->onPuse()-->onStop()-->onDestroy()
2.可視生命周期(visible lifetime):
onStart()-->onResume()-->onPause()-->onStop()
3.前臺生命周期(foreground lifetime):
onResume()<-->onPause()
Activity的生命周期圖解
創建一個Activity,必須編寫一個類繼承Activity或者是繼承Activity的子類。
在Activity中,最重要的兩個方法是:
??onCreate() 該方法是每個Activity都必須事先的方法。當Activity被創建的時候,系統會自己主動調用該方法。在該方法中,你必須初始化你的組件。在該方法中,最重要的一件事情就是調用setContentView()方法載入你定義好的視圖組建。
??onPause()當用戶離開正在使用的Activity時,該方法會被系統自己主動調用,用戶離開一個Activity,並不代表該Activity被銷毀了。 一般在該方法中進行提交和保存用戶的數據和狀態,以便用戶再次回到該界面的時候,展示用戶的數據。
必須在manifest.xml文件裏配置,系統才可以讓您使用它。
You must declare your activity in the manifest file in order for it to be accessible to the system. To declare your activity, open your manifest file and add an element as a child of the element. For example:
<manifest ... >
<application>
<activity android:name=".ExampleActivity" />
...
</application >
...
</manifest >
An element can also specify various intent filters—using the <intent-filter> element—in order to declare how other application components may activate it.
<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
讓別的應用程序使用本程序的Activity的方法:
if you want your activity to respond to implicit intents that are delivered from other applications (and your own), then you must include an <intent-filter> that includes an element and, optionally, a element and/or a element. These elements specify the type
of intent to which your activity can respond.
Starting an Activity
You can start another activity by calling startActivity(), passing it an Intent that describes the activity you want to start.
An intent can also carry small amounts of data to be used by the activity that is started.
you want to start, using the class name such as
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
Starting an activity for a result
Sometimes, you might want to receive a result from the activity that you start. In that case, start the activity by calling startActivityForResult() (instead of startActivity()). To then receive the result from the subsequent activity, implement the onActivityResult()
callback method. When the subsequent activity is done, it returns a result in an Intent to your onActivityResult() method.
For example, perhaps you want the user to pick one of their contacts, so your activity can do something with the information in that contact. Here‘s how you can create such an intent and handle the result:
private void pickContact() {
// Create an intent to "pick" a contact, as defined by the content provider URI
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 the request went well (OK) and the request was PICK_CONTACT_REQUEST
if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {
// Perform a query to the contact‘s content provider for the contact‘s name
Cursor cursor = getContentResolver().query(data.getData(),
new String[] {Contacts.DISPLAY_NAME}, null, null, null);
if (cursor.moveToFirst()) { // True if the cursor is not empty
int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
String name = cursor.getString(columnIndex);
// Do something with the selected contact‘s name...
}
}
}
Shutting Down an Activity
You can shut down an activity by calling its finish() method. You can also shut down a separate activity that you previously started by calling finishActivity().
Activity的三種狀態:
1.Resumed
The activity is in the foreground of the screen and has user focus. (This state is also sometimes referred to as "running".)
2.Paused
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn‘t cover the entire screen. A paused activity is completely alive
(the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
3.Stopped
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window
manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.
任務棧task stack:用來記錄用戶的行為,與用戶交互的界面
棧:後進先出
隊列:先進先進先出
每個應用程序都有自己的一個task stack
任務棧的四種啟動模式:
1.standard (the default mode)
2.singleTop
3.singleTask
4.singleInstance
Activity的生命周期