Android snippets added in the wrong order after changing screen orientation
I am having an issue with the ordering of fragments when the screen orientation changes during a custom animation of my fragment transition.
If I rotate the screen at the right time, the fragments are added using MyFragment2
the position where it MyFragment1
should be.
I add my fragments like this:
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 searched for many hours for information on this issue. I saw information here on ordering transactions of multiple Android snippets , 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
I understand this is a bug with re-adding fragments during the onResume () of the activity.
How can I prevent My Activity from ordering my fragments incorrectly when it resumes?
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" />
source to share
I had the same problem, this 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()));
}
source to share
try adding snippet to 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>
source to share