getSupportFragmentManager()。findFragmentById()即使应该也不返回null

时间:2014-03-16 19:59:08

标签: android fragmentmanager

我正在为具有两个片段的平板电脑构建动态UI。

我识别哪个布局显示getSupportFragmentManager().findFragmentById(R.id.my_fragment),其中my_fragments是第二个片段,显示在主listfragment旁边。但经过一些旋转和点击后,getSupportFragmentManager()。findFragmentById()即使它不应该返回片段(显示另一个布局,只显示listfragment)。

@Override
public void onRSSMessageSelected(int position) {
    AppEngine.getInstance().setRSSMessagePosition(position);
    // The user selected the RSSMessge from the RSSMessagesList

    // Capture the detail fragment from the activity layout
    RSSMessageDetail fragment = (RSSMessageDetail)
            getSupportFragmentManager().findFragmentById(R.id.rssmessage_detail);


    // BUG: supportManager doesn't return null even if it should
    if (fragment!=null) {
        fragment.updateRSSMessageDetail();

    } else {
        fragment = new RSSMessageDetail();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

布局:

<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" />

布局土地:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:name="cz.cvut.sabattom.fragment.RSSMessagesList"
              android:id="@+id/feed_messages_list"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="cz.cvut.sabattom.fragment.RSSMessageDetail"
              android:id="@+id/rssmessage_detail"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我用方法isInLayout()

解决了这个问题
if (fragment!=null&&fragment.isInLayout()) {
    fragment.updateRSSMessageDetail();
}