Skip to content Skip to sidebar Skip to footer

Multiple Calls To Fragmenttransaction.replace() - Only One Works After Orientation Change

I am using the following code to populate my UI with 2 fragments, the containers are FrameLayout's defined in XML. This first time this code is called i.e. when the app starts, it

Solution 1:

So for anyone coming across this at a later stage...

I finally manage to fix this by creating a new instance of the fragment and restoring it's state using a Fragment.SavedState object. So:

        if (mSummaryFragment.isAdded() && mDetailsFragment.isAdded()) {
            Fragment.SavedState sumState = getSupportFragmentManager().saveFragmentInstanceState(mSummaryFragment);
            Fragment.SavedState detState = getSupportFragmentManager().saveFragmentInstanceState(mDetailsFragment);
            mSummaryFragment = new SummaryFragment();
            mSummaryFragment.setInitialSavedState(sumState);
            mDetailsFragment = new DetailsFragment();
            mDetailsFragment.setInitialSavedState(detState);
        }
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        ft.add(R.id.summary_container, mSummaryFragment);
        ft.add(R.id.details_container, mDetailsFragment);
        ft.commit();

I do not understand why this works and the old method doesn't, however this may be helpful for someone else.

Solution 2:

Try using android:configChanges="orientation|keyboardHidden|screenSize" in android-manifest

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

Source : Documentation

Hence, also add "|screenSize" to configChanges if your application targets API 13 and above

Solution 3:

this should work and orientation change will not affect the fragment., if you face any problem just let me know.

publicclassMainActivityextendsFragmentActivity {
     Fragment fragment = newFragment1();
     Fragment fragment2=newFragment2();
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

          FragmentManager fm = super.getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.frame1, fragment);
            ft.replace(R.id.frame2, fragment2);
            ft.commit();

    }
    publicvoidonSaveInstanceState(Bundle outState){
        getSupportFragmentManager().putFragment(outState,"fragment1",fragment);
        getSupportFragmentManager().putFragment(outState,"fragment2",fragment2);
    }
    publicvoidonRetoreInstanceState(Bundle inState){
        fragment = getSupportFragmentManager().getFragment(inState,"fragment1");
        fragment2 = getSupportFragmentManager().getFragment(inState,"fragment2");

    }
     classFragment1extendsFragment{

            @OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {

                super.onActivityCreated(savedInstanceState);
            }
        ListView listView;
            @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View view=inflater.inflate(R.layout.summary_view,container,false);
                return view;
            }

            @OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState) {

                super.onViewCreated(view, savedInstanceState);
            }

        }

     classFragment2extendsFragment{

            @OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {

                super.onActivityCreated(savedInstanceState);
            }
        ListView listView;
            @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View view=inflater.inflate(R.layout.detail_view,container,false);
                return view;
            }

            @OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState) {

                super.onViewCreated(view, savedInstanceState);
            }

        }

}

Post a Comment for "Multiple Calls To Fragmenttransaction.replace() - Only One Works After Orientation Change"