屏幕方向更改后,Android片段按错误顺序添加

时间:2015-06-06 00:51:34

标签: android android-fragments

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

如果我在恰当的时间旋转屏幕,则会在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 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" />

2 个答案:

答案 0 :(得分:2)

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

gm.ListData = context.Companies.ToList()
                     .Where(a => a.AspNetUser.Email.NullableContains(searchTerm) ||
                                 a.TitleTranslation.TranslationTexts
                                  .Where(b => b.Text.NullableContains(searchTerm)).Any()
                    ).Select(c => new ListCompany
                    {
                        CompanyID = c.CompanyID,
                        EmailID = c.AspNetUser.Email,
                        Title = c.TitleTranslation.TranslationTexts.FirstOrDefault(d => d.Locale == Locale).Text
                    }).ToList();

答案 1 :(得分:0)

尝试在活动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>
相关问题