Reputation: 446
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 time, the fragments are added with the MyFragment2
in the position where MyFragment1
should be.
I am adding my fragments as follows:
final FragmentManager fm = activity.get().getFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(
R.animator.slide_in_left,
R.animator.slide_out_top,
R.animator.slide_in_bottom,
R.animator.slide_out_right);
ft.replace(R.id.container,
MyFragment1.newInstance(), MyFragment1.TAG);
ft.add(R.id.container,
MyFragment2.newInstance(),MyFragment12.TAG)
.addToBackStack(null)
.commit();
I have been searching for many hours for information about this problem. I have seen information here Android multiple fragment transaction ordering, here https://code.google.com/p/android/issues/detail?id=69403&thanks=69403&ts=1399482444 and here https://code.google.com/p/android/issues/detail?id=31116
My understanding is that this is a bug with re-adding fragments during onResume() of an activity.
How can I prevent my Activity from incorrectly ordering my fragments when it is resumed?
My Activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" />
Upvotes: 3
Views: 1114
Reputation: 3857
I had the same issue, the following workaround helped:
@Override
public void onResume() {
super.onResume();
bringToFrontIfNeeded();
}
@Override
public void onBackStackChanged() {
bringToFrontIfNeeded();
}
private void bringToFrontIfNeeded() {
// A fix for a weird google bug
if (amIOnTop() && (getView() != null)) {
getView().bringToFront();
}
}
private void amIOnTop() {
// depends on you app logic, can be something like
FragmentManager manager = getFragmentManager();
int count = manager.getBackStackEntryCount();
return (BACK_STACK_ENTRY_NAME.equals(manager.getBackStackEntryAt(count - 1).getName()));
}
Upvotes: 2
Reputation: 5618
try adding the fragment in the activity xml layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android: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>
Upvotes: 0