简体   繁体   English

重新创建活动后恢复片段

[英]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. 在我的应用程序中,我有一个具有“片段容器”的活动,并且当用户进入该活动时,我正在向容器中添加一个片段(FragmentOne)。 Once they enter some data in FragmentOne, I am replacing this fragment with FragmentTwo. 一旦他们在FragmentOne中输入了一些数据,我就用FragmentTwo替换此片段。

When I am on the FragmentTwo, If I take app to the background and come back, it goes to FragmentOne. 当我在FragmentTwo上时,如果我将应用程序带到后台再回来,它将转到FragmentOne。 I want it to back to FragmentTwo. 我希望它回到FragmentTwo。 I tried saving instance of current fragment and restoring it. 我尝试保存当前片段的实例并还原它。 However, backStackEntryCount increases and it causes issues when I press the back button. 但是,backStackEntryCount增加,当我按下“后退”按钮时,这会导致问题。

What is the best way to achieve this? 实现此目标的最佳方法是什么?

activity.xml 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. 在加载第一个片段的位置检查此标志,如果凸起,则加载第二个片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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