Android 4學習(6):概述
參考:《Professional
Android 4 Application Development》
深入瞭解Android Activity
每一個Android Activity都對應於一個使用者介面(UI)。每個Android Application都有一個main Activity,而這個main Activity大多由多個Fragment組成,而這些Fragment後面往往都由一個或多個secondary Activity支援。當用戶在不同的介面(視窗)切換時,Android會生成對應的新Activity。
建立Activity
Android提供了Activity類,在編寫自己的
package cn.jubincn.activities;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
空白的Activity對應的是一個空白螢幕,因此我們需要新增Fragment,Layout和View等UI元素來定製自己的Activity。大部分Activity會佔據整個螢幕,除此之外,還有一些半透明的Activity和浮動的Activity。
Android應用程式中,使用者互動介面和資料的顯示是由View來提供的。Android中的layout類,也叫ViewGroup,可以將View打包進行管理。Fragment則用來組織介面元素,從而更方便地適配不同的介面。給Activity設定介面的方法有兩種,一種是建立View類,將其設定到Activity中;另一種是將layout
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
<activity android:label="@string/app_name" android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activity生命週期
Activity的生命週期可以決定Process的優先順序,並且Activity需要對生命週期事件進行適當地響應才能提供更好的使用者體驗。
Activity Stacks
Activity的狀態決定於它在Activity Stack中的位置。Activity Stack遵循Stack的“先進後出”的規則:當一個Activity剛被建立時,它會移到棧頂;若使用者點選”go back”按鈕,或關閉當前Activity時,棧頂的Activity會被彈出,第二個Activity成為新的棧頂Activity。
Activity State
Activity的生命週期中,有這幾種狀態:
- Active:在棧頂的Activity,具有最高的優先順序,Android會竭力滿足它對資源的需求。當其他Activity變為Active狀態時,此Activity的狀態會變為Paused。
- Paused:處於這個狀態的Activity的部分或全部對使用者可見,但無法接受使用者的輸入。
- Stopped:當Activity對使用者不可見時,它將轉為Stopped狀態。此時儘管Activity仍會保留在記憶體中,但當系統資源緊張時,它會被回收掉。
- Inactive:Activity在啟動之前,或被kill之後,處於Inactive狀態。
監聽並響應Activity State的變化
Activity需要在State發生變化時作出相應的反應,為此Android為Activity的生命週期事件提供了很多相關的ActionHandler,下圖展示了Activity的幾個生命週期:
對應的程式碼:
package com.paad.activities; import android.app.Activity; import android.os.Bundle; public class MyStateChangeActivity extends Activity { // Called at the start of the full lifetime. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize Activity and inflate the UI. } // Called after onCreate has finished, use to restore UI state @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Restore UI state from the savedInstanceState. // This bundle has also been passed to onCreate. Will only be called if the Activity has been killed by the system since it was last visible. } // Called before subsequent visible lifetimes for an Activity process. @Override public void onRestart(){ super.onRestart(); // Load changes knowing that the Activity has already been visible within this process. } // Called at the start of the visible lifetime. @Override public void onStart(){ super.onStart(); // Apply any required UI change now that the Activity is visible. } // Called at the start of the active lifetime. @Override public void onResume(){ super.onResume(); // Resume any paused UI updates, threads, or processes required // by the Activity but suspended when it was inactive. } // Called to save UI state changes at the end of the active lifecycle. @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save UI state changes to the savedInstanceState. // This bundle will be passed to onCreate and onRestoreInstanceState if the process is killed and restarted by the run time. super.onSaveInstanceState(savedInstanceState); } // Called at the end of the active lifetime. @Override public void onPause(){ // Suspend UI updates, threads, or CPU intensive processes that don’t need to be updated when the Activity isn't the active foreground Activity. super.onPause(); } // Called at the end of the visible lifetime. @Override public void onStop(){ // Suspend remaining UI updates, threads, or processing that aren’t required when the Activity isn’t visible. // Persist all edits or state changes // as after this call the process is likely to be killed. super.onStop(); } // Sometimes called at the end of the full lifetime. @Override public void onDestroy(){ // Clean up any resources including ending threads, // closing database connections etc. super.onDestroy(); } }