我有使用Fragment
個選項卡式應用程序,以及一個小問題。安卓:setInitialSavedState
MainActivity
(延伸FragmentActivity
- 沒有代碼保存後,對onCreate
佈局規範)包括Fragment
稱爲TabsFragment
(延伸Fragment
,實現OnTabChangeListener
)在其佈局(其容納翼片本身在FrameLayout
)時,切換出子Fragment
s使用Transaction
s。
後者子Fragments
是實際內容的狀態,當用戶打開設備,訪問另一個應用程序,接聽電話或執行其他操作時,我希望保存其狀態。目前,子Fragment
的狀態未保存,但該活動記住正在顯示哪個子Fragment
。 (因此,如果我的設備處於橫向模式並在標籤3處於活動狀態時轉動設備,則標籤3將以縱向模式顯示,重置但顯示。因此,某些狀態在未執行任何操作的情況下被保留)。
因爲我有多個佈局(橫向和縱向),清單中的android:configChanges="orientation"
不是一個選項 - 我不想使用它,因爲它是一個美化的bandaid。
我正在使用v4支持庫(rev 10),並且在淘汰API後,我碰到了FragmentManager.saveFragmentInstanceState()
和Fragment.setInitialSavedState(Fragment f)
。我已經把saveFragmentInstanceState()對每個子Fragment
在TabsFragment
重寫的onPause()
方法。我不知道,如果這個工程,因爲,只要我把setInitialSavedState
,它提供了一個IllegalStateException
- 但是當我剛剛初始化一個新的子Fragment
標籤出現這種情況甚至。它也崩潰時,我把它放入onResume
在TabsFragment
。
代碼片段:
//... this is the 'showTab' method
if(getFragmentManager().findFragmentByTag(id) == null)
{
Fragment f = null;
if(BASIC_TAB.equals(id))
{
f = new BasicTabFragment();
f.setInitialSavedState(basicState); // basicState was set in onPause()
getFragmentManager().beginTransaction().replace(tabNo, f, tabID).commit();
}
}
如果類似的代碼放置在onResume
,我得到以下錯誤:
錯誤片段: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to resume activity (org.example.App/org.example.App/MainActivity): java.lang.IllegalStateException: Fragment already active
-snip-
Caused by: java.lang.IllegalStateException: Fragment already active at android.support.v4.app.Fragment.setIniailSavedState(Fragment.java:507)
at org.example.App.TabsFragment.onResume(TabsFragment.java:223)
etc.
Clearly I'm calling setInitialSavedState at the wrong point in the lifecycle, but it's not clear to me when this should happen.
Also I'm beginning to wonder if FragmentManager.saveFragmentInstanceState() is indeed the best thing to use here given the somewhat complex nature of the tabbed setup. If so, what to use? How can I save the state of my application's tabs?
我不認爲你應該setInitialSavedState搞亂。你是否簡單地嘗試覆寫onSaveInstanceState()?這應該是調用允許您保存任何額外的狀態信息。 – jsmith