L
L
Lordao2018-07-27 11:56:37
Android
Lordao, 2018-07-27 11:56:37

How to display nested fragment after return?

There is a main activity MainActivity , in which the framelayout is located

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>

A fragment with navigation is placed there, where nested fragments are switched by tabs.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/navigationContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/tabLayout" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        style="@style/AppTabLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentBottom="true"
        android:background="#FFFFFF"
        android:elevation="@dimen/size_medium"
        tools:targetApi="lollipop">

        <android.support.design.widget.TabItem
            android:layout_width="@dimen/size_large"
            android:layout_height="@dimen/size_large"
            android:icon="@drawable/ic_nav_main" />

        <android.support.design.widget.TabItem
            android:layout_width="@dimen/size_large"
            android:layout_height="@dimen/size_large"
            android:icon="@drawable/ic_nav_popular" />

        <android.support.design.widget.TabItem
            android:layout_width="@dimen/size_large"
            android:layout_height="@dimen/size_large"
            android:icon="@drawable/ic_nav_favorites" />

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

</RelativeLayout>

I change the fragments like this:
val transaction = fragmentManager?.beginTransaction()
transaction?.replace(R.id.navigationContainer, fragment, screenKey)
?.addToBackStack(screenKey)?.commit()

When I need to display a fragment without bottom navigation, I change the fragment not through navigationContainer, but through mainContainer.
val transaction = fragmentManager?.beginTransaction()
transaction?.replace(R.id.mainContainer fragment, screenKey)
?.addToBackStack(screenKey)?.commit()

And when we press the back button, we have a navigation fragment displayed, but the nested fragment in the navigationContainer container becomes empty.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-07-27
@Lordao

In order for this to work correctly, nested fragments (those added to R.id.navigationContainer) need to be added not through the main FragmentManager, but through the ChildFragmentManager, which can be obtained from the fragment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question