关于activity的方法onSaveInstanceState(Bundle)和onRestoreInstanceState(Bundle)).
<wbr style="line-height:25px"><span style="line-height:1.3em">protectedvoid</span><wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px"><span style="line-height:1.3em">onSaveInstanceState</span><wbr style="line-height:25px"></wbr></span><span style="line-height:1.3em">(<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Bundle</a><wbr style="line-height:25px">outState)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> Calledtoretrieveper-instancestatefromanactivitybeforebeingkilledsothatthestatecanberestoredin<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onCreate(Bundle)</a><wbr style="line-height:25px">or<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onRestoreInstanceState(Bundle)</a><wbr style="line-height:25px">(the<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Bundle</a><wbr style="line-height:25px">populatedbythismethodwillbepassedtoboth).<br style="line-height:25px"> Thismethodiscalledbeforeanactivitymaybekilledsothatwhenitcomesbacksometimeinthefutureitcanrestoreitsstate.Forexample,ifactivityBislaunchedinfrontofactivityA,andatsomepointactivityAiskilledtoreclaimresources,activityAwillhaveachancetosavethecurrentstateofitsuserinterfaceviathismethodsothatwhentheuserreturnstoactivityA,thestateoftheuserinterfacecanberestoredvia<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onCreate(Bundle)</a><wbr style="line-height:25px">or<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onRestoreInstanceState(Bundle)</a><wbr style="line-height:25px">.<br style="line-height:25px"> Donotconfusethismethodwithactivitylifecyclecallbackssuchas<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPause()</a><wbr style="line-height:25px">,whichisalwayscalledwhenanactivityisbeingplacedinthebackgroundoronitswaytodestruction,or<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStop()</a><wbr style="line-height:25px">whichiscalledbeforedestruction.Oneexampleofwhen<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPause()</a><wbr style="line-height:25px">and<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStop()</a><wbr style="line-height:25px">iscalledandnotthismethodiswhenausernavigatesbackfromactivityBtoactivityA:thereisnoneedtocall<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState(Bundle)</a><wbr style="line-height:25px">onBbecausethatparticularinstancewillneverberestored,sothesystemavoidscallingit.Anexamplewhen<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPause()</a><wbr style="line-height:25px">iscalledandnot<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState(Bundle)</a><wbr style="line-height:25px">iswhenactivityBislaunchedinfrontofactivityA:thesystemmayavoidcalling<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState(Bundle)</a><wbr style="line-height:25px">onactivityAifitisn'tkilledduringthelifetimeofBsincethestateoftheuserinterfaceofAwillstayintact.<br style="line-height:25px"> ThedefaultimplementationtakescareofmostoftheUIper-instancestateforyoubycalling<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/view/View.html#onSaveInstanceState%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState()</a><wbr style="line-height:25px">oneachviewinthehierarchythathasanid,andbysavingtheidofthecurrentlyfocusedview(allofwhichisrestoredbythedefaultimplementationof<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onRestoreInstanceState(Bundle)</a><wbr style="line-height:25px">).Ifyouoverridethismethodtosaveadditionalinformationnotcapturedbyeachindividualview,youwilllikelywanttocallthroughtothedefaultimplementation,otherwisebepreparedtosaveallofthestateofeachviewyourself.<br style="line-height:25px"> Ifcalled,thismethodwilloccurbefore<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStop()</a><wbr style="line-height:25px">.Therearenoguaranteesaboutwhetheritwilloccurbeforeorafter<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPause()</a><wbr style="line-height:25px">.<br style="line-height:25px"> 如果onSaveInstanceState要被调用的话,只能保证它在stop之前调用。但它可以在onPause()之前OR之后调用。<br style="line-height:25px"><span style="line-height:1.3em">注:应该说active要被杀死才调用该函数。但是按back键不会调用。</span><wbr style="line-height:25px">通过finish()结束active<span style="color:#000080; line-height:25px"><wbr style="line-height:25px">也不会调用onSaveInstanceState。因为既然销毁后无法重生,就没必要调用onSaveInstanceState来保存<wbr style="line-height:25px"></wbr></wbr></span><span style="color:#000080; line-height:25px"><wbr style="line-height:25px"></wbr></span><span style="line-height:1.3em"><span style="color:#000080; line-height:25px">protected</span><span style="line-height:25px"></span></span> <div style="line-height:25px"> <span style="line-height:1.3em"><span style="line-height:25px">void</span></span><wbr style="line-height:25px"><span style="line-height:25px"></span><strong>onRestoreInstanceState</strong><wbr style="line-height:25px"><span style="line-height:25px"></span><span style="line-height:1.3em; font-weight:bold">(<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Bundle</a><wbr style="line-height:25px">savedInstanceState)</wbr></span><wbr style="line-height:25px"><span style="line-height:25px"></span></wbr></wbr></wbr> </div> <div style="line-height:25px">Thismethodiscalledafter<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStart%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none; font-weight:bold">onStart()</a><wbr style="line-height:25px"><span style="line-height:25px"></span>whentheactivityisbeingre-initializedfromapreviouslysavedstate,givenhereinsavedInstanceState.Mostimplementationswillsimplyuse<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onCreate(Bundle)</a><wbr style="line-height:25px">torestoretheirstate,butitissometimesconvenienttodoithereafteralloftheinitializationhasbeendoneortoallowsubclassestodecidewhethertouseyourdefaultimplementation.Thedefaultimplementationofthismethodperformsarestoreofanyviewstatethathadpreviouslybeenfrozenby<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState(Bundle)</a><wbr style="line-height:25px">.Thismethodiscalledbetween<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStart%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStart()</a><wbr style="line-height:25px">and<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPostCreate%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPostCreate(Bundle)</a><wbr style="line-height:25px">.<br style="line-height:25px"><br style="line-height:25px"><span style="font-family:monospace; line-height:1.3em"><span style="line-height:25px">注1</span>:</span><wbr style="line-height:25px"><span style="line-height:1.3em">savedInstanceState就是在</span><wbr style="line-height:25px"><span style="line-height:1.3em">onSaveInstanceState中报存下来的Bundle.<br style="line-height:25px"><span style="line-height:25px">注2</span>:其实也可以在oncreate中读取</span><wbr style="line-height:25px"><span style="line-height:1.3em">在</span><wbr style="line-height:25px"><span style="line-height:1.3em">onSaveInstanceState中报存下来的Bundle。但是在</span><wbr style="line-height:25px"><span style="line-height:1.3em">onRestoreInstanceState</span><wbr style="line-height:25px">中处理也有些方便的地方。至少要好看些吧。<br style="line-height:25px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr style="line-height:25px"><span style="line-height:1.3em">protectedvoid</span><wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px"><span style="line-height:1.3em">onSaveInstanceState</span><wbr style="line-height:25px"></wbr></span><span style="line-height:1.3em">(<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Bundle</a><wbr style="line-height:25px">outState)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> Calledtoretrieveper-instancestatefromanactivitybeforebeingkilledsothatthestatecanberestoredin<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onCreate(Bundle)</a><wbr style="line-height:25px">or<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onRestoreInstanceState(Bundle)</a><wbr style="line-height:25px">(the<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Bundle</a><wbr style="line-height:25px">populatedbythismethodwillbepassedtoboth).<br style="line-height:25px"> Thismethodiscalledbeforeanactivitymaybekilledsothatwhenitcomesbacksometimeinthefutureitcanrestoreitsstate.Forexample,ifactivityBislaunchedinfrontofactivityA,andatsomepointactivityAiskilledtoreclaimresources,activityAwillhaveachancetosavethecurrentstateofitsuserinterfaceviathismethodsothatwhentheuserreturnstoactivityA,thestateoftheuserinterfacecanberestoredvia<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onCreate(Bundle)</a><wbr style="line-height:25px">or<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onRestoreInstanceState(Bundle)</a><wbr style="line-height:25px">.<br style="line-height:25px"> Donotconfusethismethodwithactivitylifecyclecallbackssuchas<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPause()</a><wbr style="line-height:25px">,whichisalwayscalledwhenanactivityisbeingplacedinthebackgroundoronitswaytodestruction,or<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStop()</a><wbr style="line-height:25px">whichiscalledbeforedestruction.Oneexampleofwhen<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPause()</a><wbr style="line-height:25px">and<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStop()</a><wbr style="line-height:25px">iscalledandnotthismethodiswhenausernavigatesbackfromactivityBtoactivityA:thereisnoneedtocall<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState(Bundle)</a><wbr style="line-height:25px">onBbecausethatparticularinstancewillneverberestored,sothesystemavoidscallingit.Anexamplewhen<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPause()</a><wbr style="line-height:25px">iscalledandnot<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState(Bundle)</a><wbr style="line-height:25px">iswhenactivityBislaunchedinfrontofactivityA:thesystemmayavoidcalling<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState(Bundle)</a><wbr style="line-height:25px">onactivityAifitisn'tkilledduringthelifetimeofBsincethestateoftheuserinterfaceofAwillstayintact.<br style="line-height:25px"> ThedefaultimplementationtakescareofmostoftheUIper-instancestateforyoubycalling<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/view/View.html#onSaveInstanceState%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState()</a><wbr style="line-height:25px">oneachviewinthehierarchythathasanid,andbysavingtheidofthecurrentlyfocusedview(allofwhichisrestoredbythedefaultimplementationof<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onRestoreInstanceState(Bundle)</a><wbr style="line-height:25px">).Ifyouoverridethismethodtosaveadditionalinformationnotcapturedbyeachindividualview,youwilllikelywanttocallthroughtothedefaultimplementation,otherwisebepreparedtosaveallofthestateofeachviewyourself.<br style="line-height:25px"> Ifcalled,thismethodwilloccurbefore<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStop()</a><wbr style="line-height:25px">.Therearenoguaranteesaboutwhetheritwilloccurbeforeorafter<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPause()</a><wbr style="line-height:25px">.<br style="line-height:25px"> 如果onSaveInstanceState要被调用的话,只能保证它在stop之前调用。但它可以在onPause()之前OR之后调用。<br style="line-height:25px"><span style="line-height:1.3em">注:应该说active要被杀死才调用该函数。但是按back键不会调用。</span><wbr style="line-height:25px">通过finish()结束active<span style="color:#000080; line-height:25px"><wbr style="line-height:25px">也不会调用onSaveInstanceState。因为既然销毁后无法重生,就没必要调用onSaveInstanceState来保存<wbr style="line-height:25px"></wbr></wbr></span><span style="color:#000080; line-height:25px"><wbr style="line-height:25px"></wbr></span><span style="line-height:1.3em"><span style="color:#000080; line-height:25px">protected</span><span style="line-height:25px"></span></span> <div style="line-height:25px"> <span style="line-height:1.3em"><span style="line-height:25px">void</span></span><wbr style="line-height:25px"><span style="line-height:25px"></span><strong>onRestoreInstanceState</strong><wbr style="line-height:25px"><span style="line-height:25px"></span><span style="line-height:1.3em; font-weight:bold">(<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Bundle</a><wbr style="line-height:25px">savedInstanceState)</wbr></span><wbr style="line-height:25px"><span style="line-height:25px"></span></wbr></wbr></wbr> </div> <div style="line-height:25px">Thismethodiscalledafter<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStart%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none; font-weight:bold">onStart()</a><wbr style="line-height:25px"><span style="line-height:25px"></span>whentheactivityisbeingre-initializedfromapreviouslysavedstate,givenhereinsavedInstanceState.Mostimplementationswillsimplyuse<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onCreate(Bundle)</a><wbr style="line-height:25px">torestoretheirstate,butitissometimesconvenienttodoithereafteralloftheinitializationhasbeendoneortoallowsubclassestodecidewhethertouseyourdefaultimplementation.Thedefaultimplementationofthismethodperformsarestoreofanyviewstatethathadpreviouslybeenfrozenby<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onSaveInstanceState(Bundle)</a><wbr style="line-height:25px">.Thismethodiscalledbetween<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStart%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStart()</a><wbr style="line-height:25px">and<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onPostCreate%28android.os.Bundle%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onPostCreate(Bundle)</a><wbr style="line-height:25px">.<br style="line-height:25px"><br style="line-height:25px"><span style="font-family:monospace; line-height:1.3em"><span style="line-height:25px">注1</span>:</span><wbr style="line-height:25px"><span style="line-height:1.3em">savedInstanceState就是在</span><wbr style="line-height:25px"><span style="line-height:1.3em">onSaveInstanceState中报存下来的Bundle.<br style="line-height:25px"><span style="line-height:25px">注2</span>:其实也可以在oncreate中读取</span><wbr style="line-height:25px"><span style="line-height:1.3em">在</span><wbr style="line-height:25px"><span style="line-height:1.3em">onSaveInstanceState中报存下来的Bundle。但是在</span><wbr style="line-height:25px"><span style="line-height:1.3em">onRestoreInstanceState</span><wbr style="line-height:25px">中处理也有些方便的地方。至少要好看些吧。<br style="line-height:25px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>