Activity重生机制
来源:互联网 发布:淘宝买家怎么申诉 编辑:程序博客网 时间:2024/04/27 22:44
暂时我只知道,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(),
我尝试过写个程序进行测试,但是失败了。因为该程序并不能耗掉android的大量内存。一个进程占用的内存有一定数目限制的。普通的应用程序是3M.
1,调用了finish()系列的函数.
上述情况其实系统已经很明确的知道该Activity不用重生。所以就没调用onSaveInstanceState(Bundle b)来保存信息,当然也不可能重生。
注意1:
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)保存的信息来实现重生。
因为内存原因,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框是向左和下都走了。
以前认为“调用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>
注意: 通过该方式并没有杀死不可见的进程(应用程序)
测试程序如下:
文件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
{
try{
}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>