Android 尽管不在布局中,但仍在创建片段
我在这里遵循教程:当我将设备旋转到横向,然后旋转到纵向,然后再旋转回横向时,收到一个崩溃。崩溃发生在以下代码段中Android 尽管不在布局中,但仍在创建片段,android,android-fragments,Android,Android Fragments,我在这里遵循教程:当我将设备旋转到横向,然后旋转到纵向,然后再旋转回横向时,收到一个崩溃。崩溃发生在以下代码段中 public void onArticleSelected(int position) { ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager() .findFragmentById(R.id.article_fragment); if (ar
public void onArticleSelected(int position) {
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager()
.findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// Crash happens here.
articleFrag.updateArticleView(position);
} else {
//...
}
}
}
原因是,当应用程序旋转回纵向时,片段管理器返回一个片段,尽管它不在纵向布局中。为了研究这一点,我创建了一个小应用程序,其中包含如下所示的水平布局:
<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.matt.personfragments.PersonListFragment"
android:id="@+id/person_list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.matt.personfragments.PersonDetailFragment"
android:id="@+id/person_detail"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<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.matt.personfragments.PersonListFragment"
android:id="@+id/person_list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
以及如下所示的纵向布局:
<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.matt.personfragments.PersonListFragment"
android:id="@+id/person_list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.matt.personfragments.PersonDetailFragment"
android:id="@+id/person_detail"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<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.matt.personfragments.PersonListFragment"
android:id="@+id/person_list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
我已经重写了这两个片段中的构造函数来记录它们的创建。
正如所料,当第一次创建应用程序时,我看到PersonListFragment正在创建。当我旋转设备时,我看到PersonListFragment和PersonDetailFragment。到目前为止一切正常
然而,当我将设备旋转回纵向时,我看到PersonListFragment和PersonDetailFragment再次被创建。我相信这是由于片段以保存的即时状态存在。这是正确的吗?如果是,这是否意味着教程不正确?在配置更改(如旋转)期间,
活动
将保存的内容(实际上是活动的片段管理器
)是片段
s
通常,这不会导致任何问题,因为在任何方向上都使用相同的片段。但在主/细节屏幕中,细节片段仅在横向方向可见,您可能会遇到问题
首次创建细节片段后(当您旋转到横向时),该片段将存在于FragmentManager中,直到您退出活动。这是真实的,即使当你旋转回纵向。但是,在您的活动布局不包含该片段的情况下,不会添加该细节片段
因此,您可以将支票更改为:
到