【问题标题】:How to save to state of fragment on replace如何在替换时保存到片段状态
【发布时间】:2016-09-21 12:46:42
【问题描述】:

希望您能帮助我理解如何执行我的任务。

我有 MainActivity,我在其中显示一个或另一个片段(为简单起见,我们称它们为 FragmentA 和 FragmentB)。就在片段之外,我可以影响它们的内容。

例如,发生以下情况:先加载FragmentA,然后加载FragmentB(它显示条件名称State1下的内容),然后从外部将FragmentB的内容更改为State2,然后我正在加载FragmentA。

不仅需要使用返回按钮来切换 FragmentA 和 FragmentB,还需要切换不同的状态片段。切换片段全部清除刚刚添加的 addToBackStack(null)。

但是如何保存片段的状态呢?

现在,当我从外部更改片段的状态时,我调用 detach 并立即使用 addToBackStack(null) 附加。但是当按下 Back 时,我得到了 fragmentB 的最后一个状态。

小例子: 我有 FrameLayout(main_fragment_holder) 的主要活动。当用户单击 NavigationView 中的部分时,我会将 NewsFragment 或 Categories 片段显示到 main_fragment_holder (fragmentTransaction.replace())。此外,当新闻片段可见时,我可以使用工具栏中的微调器(categories_spinner)更改新闻类别。

例如用户打开 News 并将类别更改为 Cat1、Cat2、Cat3,然后使用 NavigationView 打开 CategoriesFragment。

当用户点击返回时,我想要以下内容: 分类Fragment->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>

【问题讨论】:

  • 您将 FragmentA 替换为 FragmentB,对吗?当您从 FragmentB 回到 FragmentA 时,它不会再次重新创建 FragmentA。它重用 FragmentA 的实例,您可以将状态保存在变量中。 [developer.android.com/guide/components/fragments.html#Creating] (developer.android.com/guide/components/fragments.html#Creating)
  • 不是简单地将一个片段替换为另一个片段,而是在单个片段中更改内容。例如,fragmentB 本身会显示搜索结果。我显示 fragmentA,然后 FragmentB 替换它。用户对关键字进行搜索后:Key1、Key2、Key3分别看到不同的结果。然后,我将 FragmentB 替换为 FragmentA。结果,当按回时,我需要得到以下信息: FragmentB(key3 上的结果)-> FragmentB(key2 上的结果)-> FragmentB(key1 的结果)-> FragmentA
  • 真的我不明白你的意思。你能发布你的代码来看看流程如何?
  • 我在问题描述中添加了小例子
  • 我阅读了您的示例。当您打开 NewsFragment 并打开类别 3、2、1 时。在此NewsFragment 中,您需要将步骤存储在 step_variables 中(用于手动处理后退按钮)。之后,您将 NewsFragment 替换为 CategoriesFragment。现在,您从CategoriesFragment 按下返回按钮,它将恢复您的 NewsFragment,现在您需要从存储的 step_variables 手动控制返回按钮。

标签: android android-fragments back-stack fragment-backstack


【解决方案1】:

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

我阅读了您的示例。当您打开 NewsFragment 并打开类别 3、2 时, 1.在此 NewsFragment 中,您需要将步骤存储在 step_variables(用于手动处理后退按钮)。之后你 用 CategoriesFragment 替换 NewsFragment。现在,你按下返回按钮 从 CategoriesFragment 中,它将恢复您的 NewsFragment,现在您 需要从您存储的 step_variables 手动控制返回按钮。

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多