[英]Android fragments added in wrong order after screen orientation change
在片段转换的自定义动画期间,当屏幕方向发生变化时,我遇到片段排序问题。
如果我在正确的时间旋转屏幕,该片段时添加的MyFragment2
在位置MyFragment1
应该的。
我正在添加我的片段如下:
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=69403&thanks=69403&ts=1399482444和https://code.google.com/p /安卓/问题/细节?ID = 31116
我的理解是,这是一个在活动的onResume()期间重新添加片段的错误。
如何阻止我的Activity在恢复时错误地排序我的片段?
我的活动布局
<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" />
我遇到了同样的问题,以下解决方法有所帮助:
@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()));
}
尝试在活动xml布局中添加片段
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.