How to reload fragments in backstack after changing orientation

My Android application has a registration flow setup with Activity and I load the steps as fragments into the action 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 the public interface implemented in RegisterActivity, so I know to load the next fragment of the step, and I add a new fragment to the backstack

 mFragmentTransaction.addToBackStack(mStepOne.TAG); 

now everything works fine up to 4 steps, and I can move through the steps, saving the entered data in each fragment, if it remains in the same orientation (portait)

BUT after orientation change, the fragments disappear

if I'm up to step 3, I can still click the back button and it will return, showing me a fragment that should be there if you look at that hidden snapshot of the 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 counter and tag labels are saved but not displayed. RegisterActivity onCreateView loads a fragment of terms into the view, and this is what remains in view when navigating through the stage.

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

It seems to me that the views are loaded into RegisterActivity on top of each other and cleared of orientation changes. Can I decide what I'm trying to do? or did i do it wrong? This is my first Android app :)

Greetings

+6
source share
1 answer

It seems to be caused by outdoor activities. Have you ever added android:configChanges="orientation|keyboardHidden|screenSize" to your AndroidManifest.xml to your activity ?, if you add this flag to your activity, it will not be destroyed when the orientation changes and the fragment stack do not disappear.

+6
source

Source: https://habr.com/ru/post/982017/


All Articles