2016-09-21 73 views
0

我希望您能幫助理解如何執行我的任務。如何保存到替換片段狀態

我有MainActivity中顯示一個或其他片段(爲了簡單起見,我們稱之爲FragmentA和FragmentB)。就在片段之外,我可以影響他們的內容。

例如,會發生以下情況:首先加載FragmentA,然後FragmentB(它顯示條件名稱State1下的內容),然後從外部FragmentB將其內容更改爲State2,然後加載FragmentA。

需要使用後退按鈕切換FragmentA和FragmentB,但切換不同的狀態片段。切換片段全部清除只需添加addToBackStack(null)。

但如何保存片段狀態?

現在,當我從外部更改狀態片段時,我調用detach並立即使用addToBackStack(null)附加。但是當按Back時,我會得到fragmentB的最後一個狀態。

小示例: 我有FrameLayout(main_fragment_holder)的主要活動。當用戶點擊NavigationView中的部分我show或NewsFragment或Categories片段到main_fragment_holder(fragmentTransaction.replace())。此外,當新聞片段可見我可以改變類別的新聞與微調(categories_spinner)從工具欄。

例如,用戶打開News並將類別更改爲Cat1,Cat2,Cat3,然後使用NavigationView打開CategoriesFragment。

當用戶單擊後退,我想有以下幾點: CategoriesFragment-> NewsFragment(的Cat3) - > NewsFragment(CAT2) - > NewsFragment(CAT1)

我不知道如何正確地執行它。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <FrameLayout 
     android:id="@+id/main_fragment_holder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 


    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_news" 
      android:title="News" /> 
     <item 
      android:id="@+id/nav_categories" 
      android:title="Categories" /> 

    </group> 


</menu> 

app_bar_main.xml

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 


     <Spinner 
      android:id="@+id/categories_spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

    </android.support.v7.widget.Toolbar> 


</android.support.design.widget.AppBarLayout> 

+1

你用FragmentB替換FragmentA吧?當你從FragmentB返回到FragmentA時,它不會再次重新創建FragmentA。它重用FragmentA的實例,可以將狀態保存在變量中。 [https://developer.android.com/guide/components/fragments.html#Creating](https://developer.android.com/guide/components/fragments.html#Creating) –

+0

而不是簡單地將一個片段替換爲另一個是單個片段內容的變化。例如,fragmentB本身顯示搜索結果。我顯示fragmentA,然後FragmentB將其替換。在用戶對關鍵字執行搜索之後:Key1,Key2和Key3分別看到不同的結果。然後,我再次將FragmentB替換爲FragmentA。結果,當按下時,我需要得到以下內容:FragmentB(在key3上的結果) - > FragmentB(在key2上的結果) - > FragmentB(key1的結果) - > FragmentA – Anton111111

+0

真的,我沒有明白你的觀點。你可以發佈你的代碼,看看流程如何? –

回答

0

@ roshan-shan提供了很好的解決方案。

我看過你的例子。當你打開NewsFragment並打開category 3,2, 1。在這個NewsFragment中,您需要將步驟存儲在 step_variables(用於手動回退按鈕)中。之後,你 用CategoriesFragment替換NewsFragment。現在,您按下CategoriesFragment中的按鈕 ,它將恢復您的NewsFragment,並且現在您需要從您存儲的step_variables手動控制後退按鈕。