Activity重生机制

暂时我只知道,Activity在以下情况下不能重生。
1,调用了finish()系列的函数.
上述情况其实系统已经很明确的知道该Activity不用重生。所以就没调用
onSaveInstanceState(Bundleb) 来保存信息,当然也不可能重生。
注意1:<wbr style="line-height:25px">以上情况不会没调onSaveInstanceState(Bundleb) <div style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">注意2</span>:对于Activity,默认情况下,用户按下back键会导致系统调用该<span style="color:#0000ff; line-height:25px">Acitivity</span>的<span style="color:#0000ff; line-height:25px">o</span><span style="color:#0000ff; line-height:25px">nBackPressed()</span>函数,而在该函数中又会调用其<span style="color:#ff00ff; line-height:25px">finish()</span>函数。程序员可以重写该函数来,改写activity中按下Back键后的行为。<br style="line-height:25px"><wbr style="line-height:25px">Activity只有在以下两种情况下才能重生。<br style="line-height:25px"><span style="color:#000080; line-height:25px"><span style="line-height:25px">1,</span>因为内存原因,Activity被杀死时,可以重生。<br style="line-height:25px"><span style="line-height:25px">2</span>,默认的</span><span style="line-height:25px; color:rgb(0,0,128)">ConfigurationChanges行为配置下,</span><span style="line-height:25px; color:rgb(0,0,128)">ConfigurationChanges后,Activity也会重生,它经历</span><span style="color:#993300; line-height:25px">onPause(),onStop(),andonDestroy()</span><span style="line-height:25px; color:rgb(0,0,128)">,</span></wbr></wbr></div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px">然后用</span><span style="color:#993300; line-height:25px">onSaveInstanceState(Bundle)</span><span style="color:#000080; line-height:25px">中保存的东西来在实现重生。</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">因为内存原因,</span><span style="color:#993300; line-height:25px">Activity</span><span style="color:#003366; line-height:25px">被杀死时,系统也会调用onDestroy()。但是这种情况下,系统已经调用</span><span style="color:#993300; line-height:25px">onSaveInstanceState(Bundleb)</span><span style="color:#003366; line-height:25px">来保存信息。<br style="line-height:25px"> 所以该Activity可以在</span><span style="color:#ff6600; line-height:25px">onCreate()</span><span style="color:#003366; line-height:25px">或</span><span style="color:#ff6600; line-height:25px">onRestoreInstanceState()</span><span style="color:#003366; line-height:25px">中读取</span><span style="line-height:25px; color:rgb(153,51,0)">onSaveInstanceState(Bundleb)</span><span style="line-height:25px; color:rgb(0,51,102)">保存的信息来实现重生。</span></wbr> </div> <div style="line-height:25px"> <span style="line-height:25px">注意1</span>:<span style="color:#000080; line-height:25px">其实无论什么时候,只要Activity死亡,都会调用onDestroy()。<br style="line-height:25px"> 以前认为“调用onDestroy()后,onSaveInstanceState(Bundleb)的保存信息就消亡”的想法是不正确的。</span><br style="line-height:25px"><span style="line-height:25px">注意2</span>:<span style="color:#000080; line-height:25px">虽然按照逻辑推断和google参考文档中对onDestroy()的描述,因为内存原因,Activity被杀死时,系统也会调用onDestroy()。<br style="line-height:25px"> 但是</span><span style="color:#0000ff; line-height:25px">google文档activity生命周期图</span><span style="color:#000080; line-height:25px">却与此不符合。当然也可能时我对图的理解不对。<br style="line-height:25px"> 在</span><span style="color:#0000ff; line-height:25px">google文档activity生命周期图</span><span style="color:#000080; line-height:25px">中因为内存原因,Activity被杀死时,在onstop框是向左和下都走了。</span> </div> <div style="line-height:25px"><span style="line-height:25px">google文档activity生命周期图:</span></div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px"></span> <div style="line-height:25px"><img alt="Activity重生机制 - hubingforever - 民主与科学" src="http://developer.android.com/images/activity_lifecycle.png" style="line-height:25px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:100%; margin-top:0px; margin-right:10px; margin-bottom:0px; margin-left:0px"></div> <br style="line-height:25px"> 我尝试过写个程序进行测试,但是失败了。<span style="color:#000080; line-height:25px">因为该程序并不能耗掉android的大量内存。一个进程占用的内存有一定数目限制的。普通的应用程序是3M.</span> </div> <div style="line-height:25px">这个测试程序本身并不科学。所以你可以完全忽略它。<br style="line-height:25px"> 测试程序如下:<br style="line-height:25px"> 文件1<br style="line-height:25px"> EatMemoryActivity.java<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packagecom.gameloft;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.app.Activity;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Bundle;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.util.Log;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View.OnClickListener;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.Button;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">EatMemoryActivity</span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Activity{<br style="line-height:25px"> Stringtag="hubin2";<br style="line-height:25px"> finalstaticintkMB=1024*1024;<br style="line-height:25px"> finalstaticintkKB=1024;<br style="line-height:25px"> /**Calledwhentheactivityisfirstcreated.*/<br style="line-height:25px"> @Override<br style="line-height:25px"> publicvoidonCreate(BundlesavedInstanceState){<br style="line-height:25px"> super.onCreate(savedInstanceState);<br style="line-height:25px"> setContentView(R.layout.main);<br style="line-height:25px"> Buttonbutton=(Button)findViewById(R.id.Button01);<br style="line-height:25px"> OnClickListenerlistener=newOnClickListener(){<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> eatMemory(1024*1024);<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button.setOnClickListener(listener);<br style="line-height:25px"> Buttonbutton2=(Button)findViewById(R.id.Button02);<br style="line-height:25px"> OnClickListenerlistener2=newOnClickListener(){<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> for(inti=0;i&lt;50;i++)<br style="line-height:25px"> eatMemory(kMB/100);<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button2.setOnClickListener(listener2);<br style="line-height:25px"> Buttonbutton3=(Button)findViewById(R.id.Button03);<br style="line-height:25px"> OnClickListenerlistener3=newOnClickListener(){<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> for(inti=0;i&lt;100;i++)<br style="line-height:25px"> eatMemory(kMB/1000);<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button3.setOnClickListener(listener3);<br style="line-height:25px"> Buttonbutton4=(Button)findViewById(R.id.Button04);<br style="line-height:25px"> OnClickListenerlistener4=newOnClickListener(){<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> for(inti=0;i&lt;50;i++)<br style="line-height:25px"> eatMemory(kMB/1000);<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button4.setOnClickListener(listener4);<br style="line-height:25px"> Buttonbutton5=(Button)findViewById(R.id.Button05);<br style="line-height:25px"> OnClickListenerlistener5=newOnClickListener(){<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> for(inti=0;i&lt;100;i++)<br style="line-height:25px"> eatMemory(kKB/10);<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button5.setOnClickListener(listener5);<br style="line-height:25px"> }<br style="line-height:25px"> bytetempBuffer[][]=newbyte[10000][];<br style="line-height:25px"> voideatMemory(intsize)<br style="line-height:25px"> {<br style="line-height:25px"> inti=0;<br style="line-height:25px"> for(i=0;i&lt;tempBuffer.length;i++)<br style="line-height:25px"> {<br style="line-height:25px"> if(tempBuffer<wbr style="line-height:25px">==null)<br style="line-height:25px"> {<br style="line-height:25px"> try{<br style="line-height:25px"></wbr></span><span style="color:#3366ff; line-height:25px">tempBuffer<wbr style="line-height:25px">=newbyte[size];<br style="line-height:25px"> }catch(Exceptione)<br style="line-height:25px"> {<br style="line-height:25px"> Log.e(tag,"erro");<br style="line-height:25px"> }<br style="line-height:25px"> break;<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> Log.i(tag,"consumememory"+(i+1)+"MB");<br style="line-height:25px"> System.gc();<br style="line-height:25px"> }<br style="line-height:25px"> }</wbr></span><br style="line-height:25px"> 文件2<br style="line-height:25px"> main.xml文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;?xmlversion="1.0"encoding="utf-8"?&gt;<br style="line-height:25px"> &lt;LinearLayoutxmlns:android="<a target="_blank" rel="nofollow" href="http://schemas.android.com/apk/res/android" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">http://schemas.android.com/apk/res/android</a><wbr style="line-height:25px">"<br style="line-height:25px"> android:orientation="vertical"<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="fill_parent"<br style="line-height:25px"> &gt;<br style="line-height:25px"> &lt;TextView<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="wrap_content"<br style="line-height:25px"> android:text="@string/hello"<br style="line-height:25px"> /&gt;<br style="line-height:25px"> &lt;Buttonandroid:text="@+string/Eat1M"android:id="@+id/Button01"android:layout_width="wrap_content"android:layout_height="wrap_content"&gt;&lt;/Button&gt;<br style="line-height:25px"> &lt;Buttonandroid:text="@+string/Eat500K"android:id="@+id/Button02"android:layout_width="wrap_content"android:layout_height="wrap_content"&gt;&lt;/Button&gt;<br style="line-height:25px"> &lt;Buttonandroid:text="@+string/Eat100K"android:id="@+id/Button03"android:layout_width="wrap_content"android:layout_height="wrap_content"&gt;&lt;/Button&gt;<br style="line-height:25px"> &lt;Buttonandroid:text="@+string/Eat50K"android:id="@+id/Button04"android:layout_width="wrap_content"android:layout_height="wrap_content"&gt;&lt;/Button&gt;<br style="line-height:25px"> &lt;Buttonandroid:text="@+string/Eat10K"android:id="@+id/Button05"android:layout_width="wrap_content"android:layout_height="wrap_content"&gt;&lt;/Button&gt;<br style="line-height:25px"> &lt;/LinearLayout&gt;</wbr></span><br style="line-height:25px"> 文件3<br style="line-height:25px"> strings.xml<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;?xmlversion="1.0"encoding="utf-8"?&gt;<br style="line-height:25px"> &lt;resources&gt;<br style="line-height:25px"> &lt;stringname="hello"&gt;HelloWorld,EatMemoryActivity!&lt;/string&gt;<br style="line-height:25px"> &lt;stringname="app_name"&gt;EatMemory&lt;/string&gt;<br style="line-height:25px"> &lt;stringname="Eat1M"&gt;eat1MBmeory&lt;/string&gt;<br style="line-height:25px"> &lt;stringname="Eat1K"&gt;Eat1kBmeomry&lt;/string&gt;<br style="line-height:25px"> &lt;stringname="Eat100K"&gt;Eat100KBMemory&lt;/string&gt;<br style="line-height:25px"> &lt;stringname="Eat500K"&gt;eat500KB&lt;/string&gt;<br style="line-height:25px"> &lt;stringname="Eat10K"&gt;eat10kB&lt;/string&gt;<br style="line-height:25px"> &lt;stringname="Eat50K"&gt;eat50KB&lt;/string&gt;<br style="line-height:25px"> &lt;/resources&gt;</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意:</wbr></span>通过该方式并没有杀死不可见的进程(应用程序)<wbr style="line-height:25px"><br style="line-height:25px"></wbr> </div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值