简体   繁体   中英

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

I have an activity with a FrameLayout that contains 3 fragments.

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

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

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

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