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>
@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.