2015年2月21日 星期六

[Android] supportv4 fragment camera 問題

第一次使用supportv4 TabManager TabHost的功能來製作fragment分頁,在呼叫內建相機返回之後,有機率遇到java.lang.IllegalStateException: Fragment already added:的錯誤,網路上有很多相關討論,但感覺和此並沒有很相關。試了一陣子,發現原來是內建相機預設是橫向模式(landscape),照出來的照片會自動轉+-90度,在返回fragment的時候,這照片有機會自己再轉-+90度,(即使手機的畫面旋轉關閉),導致(個人猜測)supportv4誤判而再把fragment全部加回去,造成錯誤。

解決方法:因為此app只打算直式使用,所以manifest加入
<activity android:name="xxx.xxx.YourFragmentActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|screenSize"/>

It's my first time to use the function of supportv4 TabManager TabHost to make fragments. When calling the camera and back to the fragment, the error "java.lang.IllegalStateException: Fragment already added:" may occurs. There are many discussion about this error, but few about this case. After trying for hours, I suppose the reason is: The default android camera is landscape. After taking a picture, the picture may automatically rotates +-90 degree(even the auto rotation is off). When back to a fragment, the picture rotates -+90 degree again. supportv4 may make a wrong decision to add all the fragment back and error occurs.

Solution:Since this app is design for portrait mode, add these in manifest
<activity android:name="xxx.xxx.YourFragmentActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|screenSize"/>

沒有留言:

張貼留言