Android 使用refs.xml时调用了两次Master/Detail片段

Android 使用refs.xml时调用了两次Master/Detail片段,android,android-fragments,Android,Android Fragments,我在我的应用程序上使用主/细节设计,我希望同时支持手机和平板电脑。当我尝试使用refs.xml引用xml时,我遇到了一个奇怪的行为。当我使用这种方法时,我的ListFragment会被调用两次,下面是我设备上的Logcat com.project.android D/MainActivity﹕ onCREATE com.project.android D/ContactList﹕ onAttach com.project.android D/ContactList﹕ onActivityCrea

我在我的应用程序上使用主/细节设计,我希望同时支持手机和平板电脑。当我尝试使用refs.xml引用xml时,我遇到了一个奇怪的行为。当我使用这种方法时,我的ListFragment会被调用两次,下面是我设备上的Logcat

com.project.android D/MainActivity﹕ onCREATE
com.project.android D/ContactList﹕ onAttach
com.project.android D/ContactList﹕ onActivityCreated
com.project.android D/ContactList﹕ onAttach
com.project.android D/ContactList﹕ onActivityCreated
com.project.android D/ContactList﹕ onStart
com.project.android D/ContactList﹕ onStart
com.project.android D/ContactList﹕ onResume
com.project.android D/ContactList﹕ onResume
这是预期的行为吗?如何避免双重初始化

我将phone refs.xml放在布局文件夹中,下面是我的refs.xml

<resources>
    <item name="activity_masterdetail" type="layout">@layout/contacts_phone</item>
</resources>
这是我的布局

contacts_phone.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
contacts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:showDividers="middle">

    <fragment android:name="com.project.android.fragment.ContactsListFragment"
              android:id="@+id/fragment_container"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.project.android.fragment.ContactDetailsFragment"
              android:id="@+id/contact_detail_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

您确定这是ContactsListFragment的同一个实例吗?另外,你能发布你的实际布局吗?它会调用两次,因为你的编码就是这样。在ur xml中,u应该被设置为一个片段。当ur xml第一次运行时,将进行初始化并调用片段。再次创建新对象并分配给片段管理器。我认为这取决于:例如,如果这个R.id.fragment_容器是一个FrameLayout,那么他应该很好。但是,如果这本身就是一个ContactListDetail,我认为这将导致创建两个片段。感谢你们两位的输入,@HarshaVardhan您是正确的,当使用平板电脑时,当xml膨胀时,视图将初始化。我添加了一个检查,以查看片段是否可用于findFragmentById,并仅在不存在时创建它。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:showDividers="middle">

    <fragment android:name="com.project.android.fragment.ContactsListFragment"
              android:id="@+id/fragment_container"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.project.android.fragment.ContactDetailsFragment"
              android:id="@+id/contact_detail_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>