屏幕方向更改后,Android片段以错误的顺序添加 [英] 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.
如果我恰好在正确的时间旋转屏幕,则会在 MyFragment1
应该位于的位置添加 MyFragment2
片段.
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();
我已经搜索了许多小时,以获取有关此问题的信息.我在 Android多片段交易订单(在这里 https://code.google.com/p/android/issues/detail?id=31116
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
我的理解是,这是在活动的onResume()期间重新添加片段的错误.
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?
我的活动布局
<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" />
推荐答案
我遇到了同样的问题,以下解决方法有所帮助:
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()));
}
这篇关于屏幕方向更改后,Android片段以错误的顺序添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!