简体   繁体   中英

Resuming the fragment after recreating Activity

In my app, I have an activity that has Fragment Container, and I am adding a Fragment (FragmentOne) to container when user came to the activity. Once they enter some data in FragmentOne, I am replacing this fragment with FragmentTwo.

When I am on the FragmentTwo, If I take app to the background and come back, it goes to FragmentOne. I want it to back to FragmentTwo. I tried saving instance of current fragment and restoring it. However, backStackEntryCount increases and it causes issues when I press the back button.

What is the best way to achieve this?

activity.xml

<?xml version="1.0" encoding="utf-8"?>

<include
    android:id="@+id/toolbar"
    layout="@layout/include_toolbar" />

<RelativeLayout
    android:id="@+id/fragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar">

</RelativeLayout>

If you set a flag in your Activity indicating whether the 1st fragment has been completed, you can check this flag to see which fragment to load so the correct one is loaded when you return to the app. Check this flag where you load the 1st fragment, and if it is raised, load the 2nd fragment instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM