1

I have one activity with many fragments. If I call fragment B from fragment A(A background, B foreground) and then change the orientation of my device, fragments are showed in different order: A is in foreground and B in background. If I press back, fragment B is detached so I assume that the position into the backstack is fine. How could I restore fragments in the right order? I can't use android:configChanges="orientation|screenSize"

Thanks

Blodhgard
  • 9,130
  • 3
  • 24
  • 36

1 Answers1

0

By your post, I found that I have the exact problem as you.

The fragment will reload when the configuration change, so when the orientation change. Therefore you need to realod the view. I used the post to find the solution.Change fragment layout on orientation change!

Here is the solution that works for me. Hope it will help you.

public class YourFragmentActivity extends Fragment {

    private FrameLayout frameLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        frameLayout = new FrameLayout(getActivity());
        LayoutInflater inflater2 = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        frameLayout.addView(ReloadView(inflater2));
        return frameLayout;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        frameLayout. removeAllViews();
        LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        frameLayout .addView(ReloadView(inflater));
    }

    private View ReloadView(LayoutInflater inflater) {
        View v = inflater.inflate(R.layout.your_layout, null);
        //do your staff, button, listener, etc
        return v;
    }

}

And dont forget to add that in you manifest, otherwise it will not work.

<activity android:name="com.example.YourFragmentActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden"
        android:configChanges="orientation|screenSize"/>
Community
  • 1
  • 1
JavaJade
  • 199
  • 4
  • 13