Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android官方文檔翻譯 十七 4.1Starting an Activity

Android官方文檔翻譯 十七 4.1Starting an Activity

編輯:關於android開發

Android官方文檔翻譯 十七 4.1Starting an Activity


Starting an Activity

開啟一個Activity

This lesson teaches you to

這節課教給你

Understand the Lifecycle Callbacks

理解生命周期回調

Specify Your App’s Launcher Activity

聲明你的App的啟動Activity

Create a New Instance

創建一個新的實例

Destroy the Activity

銷毀Activity

You should also read

你還應該閱讀

Activities

Unlike other programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.

和其他的編程范式:應用程序通過一個main()方法來開啟不一樣,Android系統通過在一個Activity實例中喚醒與回調方法狀態相匹配的指定的回調方法來開始代碼的運行。有一系列的回調方法可以開啟一個activity,以及一系列的回調方法銷毀一個activity。

This lesson provides an overview of the most important lifecycle methods and shows you how to handle the first lifecycle callback that creates a new instance of your activity.

這節課提供了一些最重要的生命周期方法的概述,以及向你展現如何處理第一個生命周期回調:在你的activity中創建一個新的實例。

Understand the Lifecycle Callbacks

理解生命周期回調

During the life of an activity, the system calls a core set of lifecycle methods in a sequence similar to a step pyramid. That is, each stage of the activity lifecycle is a separate step on the pyramid. As the system creates a new activity instance, each callback method moves the activity state one step toward the top. The top of the pyramid is the point at which the activity is running in the foreground and the user can interact with it.

在一個activity的生命中,系統會按順序調用一系列核心的生命周期方法,就像一個階梯型金字塔一樣。也就是說,activity的生命周期的每種狀態都是金字塔中單獨的一步。隨著系統創建了一個新的activity實例,每個回調方法會把這個activity的狀態移向金字塔頂端。金字塔的頂端是正運行在前台的activity,用戶可以和它交互。

As the user begins to leave the activity, the system calls other methods that move the activity state back down the pyramid in order to dismantle the activity. In some cases, the activity will move only part way down the pyramid and wait (such as when the user switches to another app), from which point the activity can move back to the top (if the user returns to the activity) and resume where the user left off.

當用戶離開這個activity時,系統會調用其他的方法來把這個activity退回到底部以解除這個activity。在某些情況下,activity只有部分會移動到金字塔的底部然後等待(比如當用戶選擇了另一個應用程序時),在該點activity會返回到金字塔頂部(如果用戶要返回該activity時),然後從用戶離開的地方重新開始。

\

Figure 1. A simplified illustration of the Activity lifecycle, expressed as a step pyramid. This shows how, for every callback used to take the activity a step toward the Resumed state at the top, there’s a callback method that takes the activity a step down. The activity can also return to the resumed state from the Paused and Stopped state.<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxibG9ja3F1b3RlPg0KCTxwPs28MaGjINK7uPa52NPaQWN0aXZpdHnJ+sP81tzG2rXEvPK7r7LlzbyjrLHtz9bOqtK7uPa918zd0M298NfWy/6ho9XiwO/Vuc/Wwcu21NPauf3IpdTatqWyv7DRYWN0aXZpdHm007alsr/SxrW9UmVzdW1lo6i8zND4o6zSsr7NysfUy9DQo6nXtMystcTDv9bWu9i196OstrzT0NK71ta72LX3t723qL/J0tTIw2FjdGl2aXR5t7W72MiloaPV4rj2YWN0aXZpdHnSsr/J0tS001BhdXNlZCjU3c2jKbrNU3RvcGVko6jNo9a5o6nXtMyst7W72LW9UmVzdW1lZNe0zKyhozwvcD4NCjwvYmxvY2txdW90ZT4NCjxwPkRlcGVuZGluZyBvbiB0aGUgY29tcGxleGl0eSBvZiB5b3VyIGFjdGl2aXR5LCB5b3UgcHJvYmFibHkgZG9uJnJzcXVvO3QgbmVlZCB0byBpbXBsZW1lbnQgYWxsIHRoZSBsaWZlY3ljbGUgbWV0aG9kcy4gSG93ZXZlciwgaXQmcnNxdW87cyBpbXBvcnRhbnQgdGhhdCB5b3UgdW5kZXJzdGFuZCBlYWNoIG9uZSBhbmQgaW1wbGVtZW50IHRob3NlIHRoYXQgZW5zdXJlIHlvdXIgYXBwIGJlaGF2ZXMgdGhlIHdheSB1c2VycyBleHBlY3QuIEltcGxlbWVudGluZyB5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Pg0KPC9ibG9ja3F1b3RlPg0KPGgyIGlkPQ=="specify-your-apps-launcher-activity">Specify Your App’s Launcher Activity

指定你的應用程序的啟動Activity

When the user selects your app icon from the Home screen, the system calls the onCreate() method for the Activity in your app that you’ve declared to be the “launcher” (or “main”) activity. This is the activity that serves as the main entry point to your app’s user interface.

當用戶從設備主屏幕上選擇了你的app的圖標後,系統會調用你的應用程序的Activity中的onCreate()方法,這個Activity你已經聲明為“launcher”(或者“main”)activity了。這個activity擔任了你的app的用戶界面的主啟動入口角色。

You can define which activity to use as the main activity in the Android manifest file, AndroidManifest.xml, which is at the root of your project directory.

你可以定義在Android manifest文件下的AndroidManifest.xml中定義使用哪個activity作為主activity。這是你的項目目錄的根文件。

The main activity for your app must be declared in the manifest with an that includes the MAIN action and LAUNCHER category. For example:

你的app的主activity必須在manifest中用一個來定義,其中包含MAIN動作和LAUNCHER類別。例如:


    
        
        
    

Note: When you create a new Android project with the Android SDK tools, the default project files include an Activity class that’s declared in the manifest with this filter.

注意:當你用Android SDK工具創建了一個新的Android項目時,默認的項目文件會包含一個Activity類,它會在在manifest中用這個過濾器進行聲明。

If either the MAIN action or LAUNCHER category are not declared for one of your activities, then your app icon will not appear in the Home screen’s list of apps.

如果你的activity沒有任何一個聲明了MAIN動作或者LAUNCHER類別,那麼你的app圖標將不會在設備主屏幕的應用程序列表中出現。

Create a New Instance

創建一個新實例

Most apps include several different activities that allow the user to perform different actions. Whether an activity is the main activity that’s created when the user clicks your app icon or a different activity that your app starts in response to a user action, the system creates every new instance of Activity by calling its onCreate() method.

大部分的app會包含幾個不同的activity,以允許用戶執行不同的動作。無論是當用戶點擊你的app圖標進入的主activity,還是其它的通過用戶的動作響應的不同activity,系統都會通過調用activity的onCreate()方法去創建一個新的Activity實例。

You must implement the onCreate() method to perform basic application startup logic that should happen only once for the entire life of the activity. For example, your implementation of onCreate() should define the user interface and possibly instantiate some class-scope variables.

你必須實現onCreate()方法,在此執行基本的應用程序啟動邏輯:在activity的整個生命活動中應該只發生一次。例如。在你實現onCreate()方法時應該定義用戶界面,有可能還得實例化一些類變量。

For example, the following example of the onCreate() method shows some code that performs some fundamental setup for the activity, such as declaring the user interface (defined in an XML layout file), defining member variables, and configuring some of the UI.

例如,下面這個onCreate()方法的例子中展示了一些代碼來運行一些對於activity的基本配置,比如定義了用戶界面(在一個XML布局文件中定義),定義了成員變量,以及一些UI的配置。

TextView mTextView; // Member variable for text view in the layout
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set the user interface layout for this Activity
    // The layout file is defined in the project res/layout/main_activity.xml file
    setContentView(R.layout.main_activity);

    // Initialize member TextView so we can manipulate it later
    mTextView = (TextView) findViewById(R.id.text_message);

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // For the main activity, make sure the app icon in the action bar
        // does not behave as a button
        ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(false);
    }
}

Caution: Using the SDK_INT to prevent older systems from executing new APIs works in this way on Android 2.0 (API level 5) and higher only. Older versions will encounter a runtime exception.

警告:使用SDK_INT來阻止舊系統運行只能在Android 2.0及以上版本才能工作的新的API。舊版本將遇到一個運行時異常。

Once the onCreate() finishes execution, the system calls the onStart() and onResume() methods in quick succession. Your activity never resides in the Created or Started states. Technically, the activity becomes visible to the user when onStart() is called, but onResume() quickly follows and the activity remains in the Resumed state until something occurs to change that, such as when a phone call is received, the user navigates to another activity, or the device screen turns off.

一旦onCreate()結束運行,系統緊接著就會陸續調用onStart()方法和onResume()方法。你的activity從來不會停留在Created或者Started狀態下。嚴格意義上說,當onStart()被調用的時候activity就變得對用戶可見了,但是緊接著onResume()會緊跟著運行,activity會保持在Resumed狀態,直到一些改變這種狀態的現象出現,比如收到了一個電話,用戶主動跳轉到另一個activity,或者設備的屏幕關閉。

In the other lessons that follow, you’ll see how the other start up methods, onStart() and onResume(), are useful during your activity’s lifecycle when used to resume the activity from the Paused or Stopped states.

在接下來的其他課程中,你將看見在你的activity生命周期期間,當用於從Paused或者Stopped狀態恢復activity的時候,其它的啟動方式onStart()和onResume()是多麼有用。

Note: The onCreate() method includes a parameter called savedInstanceState that’s discussed in the latter lesson about Recreating an Activity.

注意:onCreate()方法包含一個叫做savedInstanceState(保存實例狀態)的參數,在以後的關於重新創建一個Activity的課程中我們將會討論。

\

Figure 2. Another illustration of the activity lifecycle structre with an emphasis on the three main callbacks that the system calls in sequence when creating a new instance of the activity: onCreate(), onStart(), and onResume(). Once this sequence of callbacks complete, the activity reaches the Resumed state where users can interact with the activity until they switch to a different activity.

圖2。 另一個activity生命周期結構的圖解,它強調了當創建一個新的activity實例的時候系統依次調用的三個主要回調:onCreate(), onStart(),和 onResume()。一旦這一系列回調完成,系統就會進入Resumed狀態,這個狀態下,用戶就可以和activity進行交互了,直到用戶選擇進入另一個不同的activity中。

Destroy the Activity

銷毀Activity

While the activity’s first lifecycle callback is onCreate(), its very last callback is onDestroy(). The system calls this method on your activity as the final signal that your activity instance is being completely removed from the system memory.

activity的第一個生命周期回調是onCreate,它的最後一個回調是onDestroy()。系統在你的activity中調用這個方法的最終標志是你的activity實體已經完全從系統內存中移除了。

Most apps don’t need to implement this method because local class references are destroyed with the activity and your activity should perform most cleanup during onPause() and onStop(). However, if your activity includes background threads that you created during onCreate() or other long-running resources that could potentially leak memory if not properly closed, you should kill them during onDestroy().

大多數應用程序不需要實現這個方法,因為在onPause()和onStop()中,本地類引用已經隨著activity被銷毀了,你的activity應該也執行了大部分的清理工作。然而,如果你的activity中包含你在onCreate()中創建的後台線程或者其它的一些可能引起內存洩露的長期運行的資源,這些如果沒有被完全關閉,你應該在onDestroy()中殺死它們。

@Override
public void onDestroy() {
    super.onDestroy();  // Always call the superclass

    // Stop method tracing that the activity started during onCreate()
    android.os.Debug.stopMethodTracing();
}

Note: The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method. In some cases, such as when your activity operates as a temporary decision maker to launch another activity, you might call finish() from within onCreate() to destroy the activity. In this case, the system immediately calls onDestroy() without calling any of the other lifecycle methods.

注意:一般情況下,系統會在它調用onPause()和onStop()之後調用onDestroy(),只有一種情況例外:當你在onCreate()方法中調用finish()方法時。在某些情況下,比如當你的activity作為一個臨時的決策者去開啟另一個activity時,你可能需要從onCreate()中調用finish()去銷毀這個activity。在這種情況下,系統會立即調用onDestroy(),而不會調用其他的任何生命周期方法。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved