Skip to content Skip to sidebar Skip to footer

Android Fragments Added In Wrong Order After Screen Orientation Change

I am having a problem with fragment ordering when the screen orientation changes during a custom animation of my fragment transition. If I rotate the screen at exactly the right ti

Solution 1:

I had the same issue, the following workaround helped:

@OverridepublicvoidonResume() {
    super.onResume();

    bringToFrontIfNeeded();
}

@OverridepublicvoidonBackStackChanged() {
    bringToFrontIfNeeded();
}

privatevoidbringToFrontIfNeeded() {
    // A fix for a weird google bugif (amIOnTop() && (getView() != null)) {
        getView().bringToFront();
    }
}

privatevoidamIOnTop() {
    // depends on you app logic, can be something likeFragmentManager manager = getFragmentManager();
    int count = manager.getBackStackEntryCount();
    return (BACK_STACK_ENTRY_NAME.equals(manager.getBackStackEntryAt(count - 1).getName()));

}

Solution 2:

try adding the fragment in the activity xml layout

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"><fragmentandroid:name="com.example.android.fragments.HeadlinesFragment"android:id="@+id/headlines_fragment"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent" /><fragmentandroid:name="com.example.android.fragments.ArticleFragment"android:id="@+id/article_fragment"android:layout_weight="2"android:layout_width="0dp"android:layout_height="match_parent" /></LinearLayout>

Post a Comment for "Android Fragments Added In Wrong Order After Screen Orientation Change"