<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>onActivity &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/onactivity/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Tue, 02 Jul 2019 05:44:04 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>onActivity &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>onSaveInstanceState, onRestoreInstanceState使用</title>
		<link>https://noter.tw/1892/onsaveinstancestate-onrestoreinstancestate%e4%bd%bf%e7%94%a8/</link>
					<comments>https://noter.tw/1892/onsaveinstancestate-onrestoreinstancestate%e4%bd%bf%e7%94%a8/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 18 Jan 2012 11:00:10 +0000</pubDate>
				<category><![CDATA[手機 App]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[lifecycle]]></category>
		<category><![CDATA[onActivity]]></category>
		<category><![CDATA[onActivityResult]]></category>
		<category><![CDATA[onRestoreInstanceState]]></category>
		<category><![CDATA[onSaveInstanceState]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=1892</guid>

					<description><![CDATA[<p>這篇文章要記錄onSaveInstanceState以及onRestoreInstanceState的使用，小蛙其實對這個部分了解還不是很透徹，但是小蛙的做法的確是可以work，供大家做個參考囉！ 照&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1892/onsaveinstancestate-onrestoreinstancestate%e4%bd%bf%e7%94%a8/" data-wpel-link="internal">onSaveInstanceState, onRestoreInstanceState使用</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>這篇文章要記錄<span style="color: #ff0000;"><strong>onSaveInstanceState</strong></span>以及<span style="color: #ff0000;"><strong>onRestoreInstanceState</strong></span>的使用，小蛙其實對這個部分了解還不是很透徹，但是小蛙的做法的確是可以work，供大家做個參考囉！<br />
<span id="more-1892"></span><br />
照照明星臉上架以來直到現在(好慚愧)，一直無法擺脫拍完照片，或是從相簿選取圖片後無法使用的情況(有些是Force Close, 有些是直接跳回主畫面沒有任何反應)，這裡面的問題有很多，是小蛙一開始不太能夠完全掌握相機及相簿的使用造成的，後來慢慢的大部分的機種都可以正常使用，卻面臨到同樣機種的手機，有些可以正常使用，有些卻無法使用，例如：HTC Sensation有使用者回報正常使用，卻也有回報無法使用 … 同樣的最近小蛙面臨到的是 SE Arc S。<br />
小蛙的同事昨天跟小蛙說，他的Arc S沒有辦法使用明星臉，跟上面敘述的是一樣的問題，小蛙借了他的Arc S測試後發現，拍照前建立的tmpFile以及傳給相機的imageUri，<strong>在最後onActivityResult回來後全部都變成null</strong>，小蛙百思不得其解啊！為什麼回到主畫面之後會變成null，直到看到這篇<a href="http://stackoverflow.com/questions/7285998/camera-intent-staying-open-on-android" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">stackoverflow的文章<span class="wpel-icon wpel-image wpel-icon-6"></span></a>，文章的標題雖然看起來沒關係，但jeffamaphone的回應幫了小蛙大忙！</p>
<blockquote><p>onSaveInstanceState跟onRestoreInstanceState的官方說明如下(截錄自<a href="http://developer.android.com/reference/android/app/Activity.html" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">Activity | Android Developers<span class="wpel-icon wpel-image wpel-icon-6"></span></a>)：</p>
<h4>protected void onSaveInstanceState (<a href="http://developer.android.com/reference/android/os/Bundle.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">Bundle<span class="wpel-icon wpel-image wpel-icon-6"></span></a> outState)</h4>
<div>
<div>Since: <a href="http://developer.android.com/guide/appendix/api-levels.html#level1" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">API Level 1<span class="wpel-icon wpel-image wpel-icon-6"></span></a></div>
</div>
<div>
Called to retrieve per-instance state from an activity before being killed so that the state can be restored in <code><a href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onCreate(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> or <code><a href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onRestoreInstanceState(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> (the <code><a href="http://developer.android.com/reference/android/os/Bundle.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">Bundle<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> populated by this method will be passed to both).<br />
This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via <code><a href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onCreate(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> or <code><a href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onRestoreInstanceState(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code>.<br />
Do not confuse this method with activity lifecycle callbacks such as <code><a href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onPause()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code>, which is always called when an activity is being placed in the background or on its way to destruction, or <code><a href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onStop()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> which is called before destruction. One example of when <code><a href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onPause()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> and <code><a href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onStop()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> is called and not this method is when a user navigates back from activity B to activity A: there is no need to call <code><a href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onSaveInstanceState(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> on B because that particular instance will never be restored, so the system avoids calling it. An example when <code><a href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onPause()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> is called and not <code><a href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onSaveInstanceState(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> is when activity B is launched in front of activity A: the system may avoid calling <code><a href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onSaveInstanceState(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> on activity A if it isn&#8217;t killed during the lifetime of B since the state of the user interface of A will stay intact.<br />
The default implementation takes care of most of the UI per-instance state for you by calling <code><a href="http://developer.android.com/reference/android/view/View.html#onSaveInstanceState%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onSaveInstanceState()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of <code><a href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onRestoreInstanceState(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code>). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.<br />
If called, this method will occur before <code><a href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onStop()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code>. There are no guarantees about whether it will occur before or after <code><a href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onPause()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code>.</p>
<h4>protected void onRestoreInstanceState (<a href="http://developer.android.com/reference/android/os/Bundle.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">Bundle<span class="wpel-icon wpel-image wpel-icon-6"></span></a> savedInstanceState)</h4>
<div>
<div>Since: <a href="http://developer.android.com/guide/appendix/api-levels.html#level1" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">API Level 1<span class="wpel-icon wpel-image wpel-icon-6"></span></a></div>
</div>
<div>
This method is called after <code><a href="http://developer.android.com/reference/android/app/Activity.html#onStart%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onStart()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> when the activity is being re-initialized from a previously saved state, given here in <var>savedInstanceState</var>. Most implementations will simply use <code><a href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onCreate(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by <code><a href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onSaveInstanceState(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code>.<br />
This method is called between <code><a href="http://developer.android.com/reference/android/app/Activity.html#onStart%28%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onStart()<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code> and <code><a href="http://developer.android.com/reference/android/app/Activity.html#onPostCreate%28android.os.Bundle%29" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">onPostCreate(Bundle)<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code>.</div>
</div>
</blockquote>
<p>看得懂的就看，看不懂的google一下，對岸有很多網站對於這兩個function都有翻譯。小蛙這邊直接講照照明星臉相機及相簿無法使用的原因。<br />
點選從使用相機拍照及從相簿選擇的同時，小蛙先建了一個tmpFile，在丟入相機的參數中增加imageUri = Uri.fromFile(tmpFile);，正常情況下若從相簿挑選照片回來，小蛙會把得到的相片轉存到tmpFile(如此是為了避免修改或刪除原始照片)；假設從相機回來，可以從剛剛設定的imageUri中取得圖片，但偏偏這兩個回來的東西在一台Arc S上面正常，在另一台Arc S上面卻回傳java.lang.NullPointerException。<br />
Override了onSaveInstanceState及onRestoreInstanceState這兩個function印出Log訊息發現，兩台都會呼叫onSaveInstanceState，但是只有其中一台會呼叫onRestoreInstanceState，而這台恰巧就是會發生錯誤的那台。小蛙猜測當使用Intent開啟內建相機時(MediaStore.ACTION_IMAGE_CAPTURE)，<span style="color: #ff0000;"><strong>由於手機內部的記憶體不夠使用，造成照照明星臉主程式必須先暫時關閉，當照相動作完成後才會再回到照照明星臉，這時候照照明星臉之前建立的變數，</strong></span>例如：tmpFile, imageUri。<span style="color: #ff0000;"><strong>就會變成null</strong><span style="color: #000000;">，</span></span>解決的方法只要在Override onSaveInstanceState並且在該function中加入要保存的變數及其值，當主控權又回到照照明星臉時再呼叫onRestoreInstanceState把剛剛存起來的值還原，就可以避免掉onActivityResult()中有時候會出現java.lang.NullPointerException，有時候卻又可以正常使用的問題了！</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1892/onsaveinstancestate-onrestoreinstancestate%e4%bd%bf%e7%94%a8/" data-wpel-link="internal">onSaveInstanceState, onRestoreInstanceState使用</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/1892/onsaveinstancestate-onrestoreinstancestate%e4%bd%bf%e7%94%a8/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
