1

I hope for your help in understanding how to implement my task.

I have MainActivity in which I show one or the other fragment (for simplicity let's call them FragmentA and FragmentB). Just outside of the fragments I can influence their contents.

For example, the following happens: first load FragmentA, then FragmentB (it displays the content under the conditional name State1), then FragmentB from the outside to change its contents to State2, and then I am loading FragmentA.

Need to work the back button to switch not only FragmentA and FragmentB but different state fragments. Switching fragments all clear just added addToBackStack(null).

But how to save state of fragment?

Now when I change state a fragment from the outside I call detach and immediately attach with addToBackStack(null). But when pressing Back I get the last state of the fragmentB.

Small example: I have main activity with FrameLayout(main_fragment_holder). When user click on section in NavigationView i show or NewsFragment or Categories fragment to main_fragment_holder (fragmentTransaction.replace()). Also when News Fragment visible i can change category of news with spinner (categories_spinner) from toolbar.

For example user open News and change category to Cat1, Cat2, Cat3, then open CategoriesFragment with NavigationView.

And when user click back i want have the following: CategoriesFragment->NewsFragment(Cat3)->NewsFragment(Cat2)->NewsFragment(Cat1)

And I don't know how to properly implement it.

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>

Anton111111
  • 656
  • 1
  • 7
  • 23
  • 1
    You replace FragmentA by FragmentB, right? When you back from FragmentB to FragmentA, It won't recreate FragmentA again. It reuse instance of FragmentA, you can save states in variables. [https://developer.android.com/guide/components/fragments.html#Creating] (https://developer.android.com/guide/components/fragments.html#Creating) – RoShan Shan Sep 21 '16 at 13:20
  • Rather than simply replacing one fragment to another there is a change of content within a single fragment. For example fragmentB in itself shows search results. I display fragmentA, then FragmentB replace it. After a user performs a search on the keywords: Key1, Key2, and Key3 respectively see different results. Then again, I replace FragmentB to FragmentA. As a result, when pressing back, i need to get the following: FragmentB (with results on key3) -> FragmentB (with the results on the key2) -> FragmentB (results for key1) -> FragmentA – Anton111111 Sep 21 '16 at 14:29
  • Really I don't get your point.Could you post your codes to see how the flow process? – RoShan Shan Sep 22 '16 at 02:46
  • I've added small example in question description – Anton111111 Sep 22 '16 at 06:31
  • I read your example.When you open `NewsFragment` and open category 3, 2, 1 . In this `NewsFragment` you need to store your steps in step_variables (for manually handle back button). After that you replace `NewsFragmen`t by `CategoriesFragment`. Now, you press back button from `CategoriesFragment`, it will restore your NewsFragment and now you need to controll back button manually from step_variables you stored. – RoShan Shan Sep 22 '16 at 08:40
  • i've already done same solution. I thought that have native solution for that ;( Any way thanks. – Anton111111 Sep 22 '16 at 11:18

1 Answers1

0

@roshan-shan offered good solution.

I read your example.When you open NewsFragment and open category 3, 2, 1 . In this NewsFragment you need to store your steps in step_variables (for manually handle back button). After that you replace NewsFragment by CategoriesFragment. Now, you press back button from CategoriesFragment, it will restore your NewsFragment and now you need to controll back button manually from step_variables you stored.

Anton111111
  • 656
  • 1
  • 7
  • 23