2013年8月15日 星期四

activity life cycle

App用到的activity 必須要記錄在AndroidManifest.xml

ex:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hana"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.hanana.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
      android:name=".CheatActivity"
      android:label="@string/app_name" />
        
    </application>

</manifest>


說明:
指定android:name時,開頭的package name可以用一點簡寫,如果package name等於manifest的package name。







於App的主畫面按下back鍵:  結束App, activity的onDestroy被呼叫

按下home鍵:  App進入背景,onStop被呼叫

activity被destroy的case:

1. 當runtime configuration改變時,比方畫面轉向時,原本的activity會被destroy,產生新的activity。

2. 當系統需要更多的記憶體,而activity在paused or stopped狀態



 Changing the screen orientation destroys and re-creates the activity from scratch.

Pressing the Home key pauses the activity but does not destroy it.

 Pressing the application icon might start a new instance of the activity, even if the old one was not destroyed.

 Letting the screen sleep pauses the activity, and the screen awakening resumes it. (This is similar to taking an incoming phone call.)

沒有留言:

張貼留言