我有一个片段SearchPageFragment
(代表平板电脑上的主视图),它本身包含两个片段:
<fragment android:name="com.test.fragments.SearchFormFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:id="@+id/searchFormFragment"
android:tag="searchFormFragmentTag">
<!-- Preview: layout=@layout/fragment_search_form -->
</fragment>
<fragment android:name="com.test.fragments.SearchResultsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/searchResultsFragment">
<!-- Preview: layout=@layout/fragment_search_results -->
</fragment>
一切正常 - 页面加载,一切正常......直到你改变方向。当您执行以下错误时:
java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f08001f, tag searchFormFragmentTag, or parent id 0x0 with another fragment for com.test.fragments.SearchFormFragment
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:262)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
以下是切换方向时的调用顺序:
SearchPageFragment(4911): Creating...
SearchPageFragment(4911): Inflating...
SearchFormFragment(4911): Creating...
SearchFormFragment(4911): Inflating...
SearchResultsFragment(4911): Creating...
SearchResultsFragment(4911): Inflating...
// Change Orientation
SearchPageFragment(4911): Pausing...
SearchFormFragment(4911): Pausing...
SearchResultsFragment(4911): Pausing...
SearchPageFragment(4911): Destroying...
SearchFormFragment(4911): Destroying...
SearchResultsFragment(4911): Destroying...
// All Seems Normal - But...
SearchPageFragment(4911): Creating...
SearchPageFragment(4911): Creating...
SearchPageFragment(4911): Inflating...
SearchFormFragment(4911): Creating...
SearchFormFragment(4911): Inflating...
SearchResultsFragment(4911): Creating...
SearchResultsFragment(4911): Inflating...
SearchPageFragment(4911): Inflating...
// SearchPageFragment has started twice and crashes when the second one inflates a ***unique*** component
AndroidRuntime(4911): Shutting down VM
有人知道为什么我的容器片段在方向改变时会被调用两次吗?