【问题标题】:Android fragments added in wrong order after screen orientation change屏幕方向更改后按错误顺序添加的 Android 片段
【发布时间】:2015-06-06 00:51:33
【问题描述】:

在我的片段转换的自定义动画期间屏幕方向发生变化时,我遇到片段排序问题。

如果我在正确的时间旋转屏幕,片段会在MyFragment1 应该在的位置添加MyFragment2

我正在添加我的片段如下:

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 multiple fragment transaction ordering,这里是https://code.google.com/p/android/issues/detail?id=69403&thanks=69403&ts=1399482444,这里是https://code.google.com/p/android/issues/detail?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" />

【问题讨论】:

  • 如何在xml中按你想要的顺序添加片段
  • 您的意思是将它们预先添加到主布局中吗?目前我的活动不包含任何布局信息。我正在以编程方式交换片段,并且布局在它们自己的布局文件中单独描述。该问题仅在方向更改期间发生。
  • 是的,在活动中使用 标签来订购您的片段
  • 好的,我会试一试。泰。

标签: android android-fragments


【解决方案1】:

我遇到了同样的问题,以下解决方法有所帮助:

@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()));

}

【讨论】:

  • 不错。谢谢。我已经有一段时间没有做这个了,但这是给出的最佳解决方案。
【解决方案2】:

尝试在活动 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>

【讨论】:

  • 一开始我只有一个片段,所以我真的不想在我的 Activity 布局中定义 2 个静态片段。
  • 你是如何添加容器的,你使用的是哪个容器?
  • 使用 FragmentManager 和 FragmentTransaction。
  • 你的 R.id.container 是什么?框架布局,线性的还是相对的?
  • 我正在使用 LinearLayout。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多