暫時我只知道,Activity在以下情況下不能重生。
1,調用了finish()系列的函數.
上述情況其實系統已經很明確的知道該Activity不用重生。所以就沒調用onSaveInstanceState(Bundle b)來儲存資訊,當然也不可能重生。
注意1:以上情況不會沒調onSaveInstanceState(Bundle b)
注意2:對於Activity,預設情況下,使用者按下back鍵會導致系統調用該Acitivity的onBackPressed()函數,而在該函數中又會調用其finish()函數。程式員可以重寫該函數來,改寫activity中按下Back鍵後的行為。
Activity只有在以下兩種情況下才能重生。
1,因為記憶體原因,Activity被殺死時,可以重生。
2,預設的Configuration Changes行為配置下,Configuration Changes後,Activity也會重生,它經曆onPause(), onStop(), and onDestroy(),然後用onSaveInstanceState(Bundle)中儲存的東西來在實現重生。
因為記憶體原因,Activity被殺死時,系統也會調用onDestroy()。但是這種情況下,系統已經調用onSaveInstanceState(Bundle b)來儲存資訊。
所以該Activity可以在onCreate()或onRestoreInstanceState()中讀取onSaveInstanceState(Bundle b)儲存的資訊來實現重生。注意1:其實無論什麼時候,只要Activity死亡,都會調用onDestroy()。
以前認為“調用onDestroy()後,onSaveInstanceState(Bundle b)的儲存資訊就消亡”的想法是不正確的。
注意2:雖然按照邏輯推斷和google參考文檔中對onDestroy()的描述,因為記憶體原因,Activity被殺死時,系統也會調用onDestroy()。
但是google文檔activity生命週期圖卻與此不符合。當然也可能時我對圖的理解不對。
在google文檔activity生命週期圖中因為記憶體原因,Activity被殺死時,在onstop框是向左和下都走了。google文檔activity生命週期圖:
我嘗試過寫個程式進行測試,但是失敗了。因為該程式並不能耗掉android的大量記憶體。一個進程佔用的記憶體有一定數目限制的。普通的應用程式是3M.這個測試程式本身並不科學。所以你可以完全忽略它。
測試程式如下:
檔案1
EatMemoryActivity.java
package com.gameloft;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class EatMemoryActivity extends Activity {
String tag="hubin2";
final static int kMB=1024*1024;
final static int kKB=1024;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.Button01);
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
eatMemory(1024*1024);
}
};
button.setOnClickListener(listener);
Button button2 = (Button) findViewById(R.id.Button02);
OnClickListener listener2 = new OnClickListener() {
public void onClick(View v) {
for(int i=0;i<50;i++)
eatMemory(kMB/100);
}
};
button2.setOnClickListener(listener2);
Button button3 = (Button) findViewById(R.id.Button03);
OnClickListener listener3 = new OnClickListener() {
public void onClick(View v) {
for(int i=0;i<100;i++)
eatMemory(kMB/1000);
}
};
button3.setOnClickListener(listener3);
Button button4 = (Button) findViewById(R.id.Button04);
OnClickListener listener4 = new OnClickListener() {
public void onClick(View v) {
for(int i=0;i<50;i++)
eatMemory(kMB/1000);
}
};
button4.setOnClickListener(listener4);
Button button5 = (Button) findViewById(R.id.Button05);
OnClickListener listener5 = new OnClickListener() {
public void onClick(View v) {
for(int i=0;i<100;i++)
eatMemory(kKB/10);
}
};
button5.setOnClickListener(listener5);
}
byte tempBuffer[][]=new byte[10000][];
void eatMemory(int size)
{
int i=0;
for(i=0;i<tempBuffer.length;i++)
{
if(tempBuffer==null)
{
try{
tempBuffer=new byte[size];
}catch(Exception e)
{
Log.e(tag,"erro");
}
break;
}
}
Log.i(tag,"consume memory "+(i+1)+"MB");
System.gc();
}
}
檔案2
main.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="@+string/Eat1M" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+string/Eat500K" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+string/Eat100K" android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+string/Eat50K" android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+string/Eat10K" android:id="@+id/Button05" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
檔案3
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, EatMemoryActivity!</string>
<string name="app_name">EatMemory</string>
<string name="Eat1M">eat 1 MB meory</string>
<string name="Eat1K">Eat 1 kB meomry</string>
<string name="Eat100K">Eat 100 KB Memory</string>
<string name="Eat500K">eat 500 KB</string>
<string name="Eat10K">eat 10 kB</string>
<string name="Eat50K">eat 50 KB</string>
</resources>
注意:通過該方式並沒有殺死不可見的進程(應用程式)