我希望您能帮助理解如何执行我的任务。如何保存到替换片段状态
我有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>
你用FragmentB替换FragmentA吧?当你从FragmentB返回到FragmentA时,它不会再次重新创建FragmentA。它重用FragmentA的实例,可以将状态保存在变量中。 [https://developer.android.com/guide/components/fragments.html#Creating](https://developer.android.com/guide/components/fragments.html#Creating) –
而不是简单地将一个片段替换为另一个是单个片段内容的变化。例如,fragmentB本身显示搜索结果。我显示fragmentA,然后FragmentB将其替换。在用户对关键字执行搜索之后:Key1,Key2和Key3分别看到不同的结果。然后,我再次将FragmentB替换为FragmentA。结果,当按下时,我需要得到以下内容:FragmentB(在key3上的结果) - > FragmentB(在key2上的结果) - > FragmentB(key1的结果) - > FragmentA – Anton111111
真的,我没有明白你的观点。你可以发布你的代码,看看流程如何? –