简体   繁体   中英

How to reload fragments into the backstack after orientation change

My android app has a registration flow setup with an Activity and I load the steps as fragments into the activity layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context="com.example.android.ui.RegisterActivity">

    <LinearLayout
        android:id="@+id/register_container"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></LinearLayout>

</RelativeLayout>

Now each fragment class implements a public interface implemented in the RegisterActivity so that I know to load the next step fragment and I add the new fragment to the backstack

mFragmentTransaction.addToBackStack(mStepOne.TAG);

now it all works fine all the way through 4 steps and I can navigate back through the steps while retaining the inputted data in each fragment IF it stays in the same orientation (portait)

BUT once i change the orientation, the fragment views disappear

if I am up to step 3, I can still hit the back button and it will go back showing me the fragment that is meant to be there by viewing the backstack changed listener

mFragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            if(mFragmentManager.getBackStackEntryCount() > 0) {
                Log.d("BACK STACK", "TAG (name): " + mFragmentManager.getBackStackEntryAt( (mFragmentManager.getBackStackEntryCount() - 1)).getName());
            }
        }
    });

The count and tags of the fragments is retained but not the views. The RegisterActivity onCreateView loads the terms fragment into the view and it is what remains in view when navigating back through the steps.

mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mTerms = new RegisterFragmentTerms();
mFragmentTransaction.add(R.id.register_container, mTerms);
mFragmentTransaction.commit();

It seems to me the views are loaded into the RegisterActivity on top of each other and cleared on orientation change. Is it possible to solve what I'm trying to do? or have I implemented it wrong? It's my first android app :)

Cheers

It seems to be caused by Activity recreation. Have you ever add android:configChanges="orientation|keyboardHidden|screenSize" in your AndroidManifest.xml to your Activity? , if you add that flag to you activity, it will not be destroyed when orientation changes and the fragments stack will not disappear.

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