简体   繁体   English

当我旋转第二个片段时,它会回到第一个

[英]When I rotate the second fragment, it goes back to the first

I have an activity with a FrameLayout that contains 3 fragments.我有一个包含 3 个片段的 FrameLayout 的活动。

When I rotate in landscape the second fragment, my activity reload and i see the first fragment.当我横向旋转第二个片段时,我的活动重新加载,我看到第一个片段。

How can i avoid this problem?我怎样才能避免这个问题?

MyActivity:我的活动:

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

MyActivity.java我的活动.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_system_signup_user);
    SignUpFragment fragmentOne = new SignUpFragment();
    getSupportFragmentManager().beginTransaction().add(R.id.viewPager, fragmentOne).commit();
}

After recreation method onCreate calling again, to prevent from adding fragment to container you can do :在重新调用onCreate方法后,为了防止将片段添加到容器中,您可以执行以下操作:

...
if (savedInstanceState == null) {
            SignUpFragment fragmentOne = new SignUpFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.viewPager, fragmentOne).commit();
        }
...

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

相关问题 当我从第二个片段按下返回按钮时,如何更改第一个片段中的图像 - How to change image in first fragment when I pressed back button from the second fragment 从第二个片段到第一个片段的片段返回堆栈 - Fragment Back stack from second fragment to first fragment 如何使用导航组件从第二个片段返回到第一个片段? - How can I back from the second fragment to the first fragment using Navigation Component? 当设备在Android硬纸板中旋转90度时,如何从第二屏幕返回到第一屏幕? - How to BACK from second screen to first screen when device rotate 90 degree in android cardboard? 当用户在第二个片段中时禁用第一个片段 - Disable First Fragment When User is in Second Fragment 片段返回到指定的片段 - fragment back goes to specified fragment go 按返回时返回第一个片段 - go back to first fragment when press back 在Fragment上做点事回去 - Do something on Fragment goes back 我的设备后退按钮无法正常工作,它会转到第一个html,但返回到第二个html - My device back button is not working properly it goes to first html but return back to second html 当替换为第二个片段时,第一个片段上的按钮移出屏幕 - Button on first fragment moves out of screen when replaced with second fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM