<?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>onActivityResult &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/onactivityresult/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Tue, 29 Oct 2019 05:48:16 +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>onActivityResult &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Error about StartActivityForResult from camera intent</title>
		<link>https://noter.tw/2191/error-about-startactivityforresult-from-camera-intent/</link>
					<comments>https://noter.tw/2191/error-about-startactivityforresult-from-camera-intent/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Tue, 20 Mar 2012 15:58:00 +0000</pubDate>
				<category><![CDATA[手機 App]]></category>
		<category><![CDATA[onActivityResult]]></category>
		<category><![CDATA[android:configChanges]]></category>
		<category><![CDATA[MediaStore.ACTION_IMAGE_CAPTURE]]></category>
		<category><![CDATA[NullPointerException]]></category>
		<category><![CDATA[startActivityForResult]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2191</guid>

					<description><![CDATA[<p>之前在開發「照照明星臉」的時候，遇到最多問題的部分就在呼叫內部相機來讓使用者拍照，並在拍照後取得照片來做後續處理。小蛙在[Android] 照照明星臉 &#8211; 開發心得 @ 過去蛙齋也有提到在&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2191/error-about-startactivityforresult-from-camera-intent/" data-wpel-link="internal">Error about StartActivityForResult from camera intent</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>之前在開發「<span style="color: #ff0000;"><strong>照照明星臉</strong></span>」的時候，遇到最多問題的部分就在呼叫內部相機來讓使用者拍照，並在拍照後取得照片來做後續處理。小蛙在<strong><a href="http://blog.yam.com/carl44/article/43066648" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">[Android] 照照明星臉 &#8211; 開發心得 @ 過去蛙齋<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>也有提到在處理相機時由於<strong>行為模式</strong>不同而有所差異，但找了很久卻仍舊找不到問題所在，就在這幾天盛哥開發<strong><a href="http://ushare.cts.com.tw/" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">UShare<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong> for Android的時候也恰巧遇到這個問題，於是跟盛哥一起研究這個問題。<br />
<span id="more-2191"></span><br />
小蛙先把事情發生的經過說明清楚，照照明星臉中需要使用到「<strong>相機</strong>」功能，而最快又方便的方法就是透過<span style="color: #ff0000;"><strong>MediaStore.ACTION_IMAGE_CAPTURE</strong></span>這種Intent的方式呼叫Android內部相機提供拍照功能(可以不用大費周章自己寫一個相機功能出來)，而運作的方式是由主程式以<span style="color: #ff0000;"><strong>startActivityForResult</strong></span>啟動一個相機Intent，接著在照完相之後在<span style="color: #ff0000;"><strong>onActivityResult</strong></span>把照片的位置以及一些資訊接收回來做後續處理。(使用的方法小蛙這邊不贅述了，以「<strong>MediaStore.ACTION_IMAGE_CAPTURE</strong>」當關鍵字就可以google出一堆教學)。<br />
詭異的地方在於startActivityForResult啟動相機到拍完照片都正常，等回到主程式onActivityResult要處理相片時卻發生之前設定的Uri、tmpFile &#8230; 等等資料全部都變成null，到相簿中查看相片明明就在，可是卻沒辦法從主程式中得到設定好的相片位置(是null)。上網查了一些資料，使用的方式明明就沒錯，不知道為什麼回來後的資料通通消失。<br />
盛哥跑來找小蛙討論，但盛哥竟然忘了之前小蛙開發明星臉的時候，也問過他一個一模一樣的問題，盛哥才猛然想起並問小蛙最後是怎麼解決的，其實小蛙也不知道是怎麼解決的，也許根本就還沒有解決 &#8230; = = &#8230; 小蛙跟盛哥把生命周期都印出來，看看運行的狀況，發現在小蛙的Desire下，運作一切正常；但在盛哥的Galaxy I9000上卻會發生主程式運行了onDestory()，也因為這樣才造成所有的資料通通被清空，但兩個人想了很久還是沒有任何結論，只知道I9000做了這件事，卻對於為什麼I9000會做這件事沒有頭緒。<br />
Activity會莫名其妙被摧毀的原因，小蛙的笨腦袋想的到的就是<strong>記憶體不足</strong>，導致系統砍程式，但盛哥說根本沒裝軟體(開發機)，從應用程式頁面進去看到記憶體也還有127MB，顯然問題不在這。<br />
想了很久，小蛙突然想到一個Activity被destory的情況，就是之前在測試以下這幾篇文章的時候：<br />
<strong><a href="http://wazai.net/2109/android-tabhost-without-tabactivity" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">Android TabHost Without TabActivity<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong><br />
<strong><a href="http://wazai.net/2114/android-tabhost%e4%b8%ad%e5%88%87%e6%8f%9bactivity" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">Android TabHost中切換Activity<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong><br />
<strong><a href="http://wazai.net/2125/android-tabhost-with-fragmentactivity" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">Android Tabhost with FragmentActivity<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong><br />
雖然很早之前就知道Android在螢幕轉向的時候Activity會歷經一次生死，系統會先將原本的Activity刪除，再新增一個轉換過方向的Activity出來，但是在測試這三篇的時候才知道(遲鈍阿&#8230;)，原來可以直接透過設定<span style="color: #ff0000;"><a href="http://developer.android.com/guide/topics/manifest/activity-element.html#config" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right"><span style="color: #ff0000;"><strong><code>android:configChanges</code></strong></span><span class="wpel-icon wpel-image wpel-icon-6"></span></a></span>來讓Activity躲過一次生死輪迴(<strong><a href="http://wazai.net/2120/android-screen-orientation-event%e8%9e%a2%e5%b9%95%e6%96%b9%e5%90%91%e8%99%95%e7%90%86acitivity-liftcycle" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">可參考Android Screen Orientation Event螢幕方向處理+Acitivity Liftcycle<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>)。<br />
經過一番測試之後，盛哥已經抱著砍掉重練的想法了，小蛙也想死馬當活馬醫，試試看會不會是因為螢幕轉向的問題造成的，於是拿了I9000仔細查看每個步驟，I9000的相機畫面是「橫的」，包括按鍵及設定介面都是「橫的」，而主程式設定是「直的」，心想會不會是因為這樣子造成的。記得開發明星臉的時候，有「某些」機型無法使用(雖然不完全正常，但相對來說正常些)，以及「同機型的某些手機」可以使用，但「同機型的某些手機」卻不能使用(這個極度不正常，可能的原因太多了，包括刷機以及版本)。<br />
跟盛哥說了這個想法之後，反正也沒有什麼其他辦法了，就試試這個方法看看，結果 &#8230; 會寫這篇文章，當然就是有結果囉！果然跟小蛙想的一樣，是因為「螢幕轉向」造成這個問題，小蛙找了好久都沒有找到類似的相關文章，希望這篇文章可以幫到一些跟小蛙有著相同問題的開發者！Android碰到的問題常常不只一種原因，但在無計可施的情況下，這方法可以試試看喔！</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2191/error-about-startactivityforresult-from-camera-intent/" data-wpel-link="internal">Error about StartActivityForResult from camera intent</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2191/error-about-startactivityforresult-from-camera-intent/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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[onActivity]]></category>
		<category><![CDATA[onActivityResult]]></category>
		<category><![CDATA[onRestoreInstanceState]]></category>
		<category><![CDATA[onSaveInstanceState]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[lifecycle]]></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>
