小编hap*_*yar的帖子

为什么findFragmentById在调用替换FragmentLayout之前返回旧片段?

为什么在将片段从第一个(pageNews)更改为第二个(pageViewFromWeb)后,"findFragmentById"会返回对第一个(pageNews)片段的引用?

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:paddingTop="10dp"
    android:background="#000">

    <FrameLayout
        android:id="@+id/frgmCont"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000">
    </FrameLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

码:

   fTrans = getSupportFragmentManager().beginTransaction();
   fTrans.add(R.id.frgmCont, pageNews);
   ....
   ....    
   public void selectNews(String news_id) {
   // Toast.makeText(this, news_id, Toast.LENGTH_SHORT).show();
    fTrans = getSupportFragmentManager().beginTransaction();
    fTrans.replace(R.id.frgmCont, pageViewFromWeb);
    fTrans.commit();
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frgmCont);
    ...
}
Run Code Online (Sandbox Code Playgroud)

android android-fragments

5
推荐指数
1
解决办法
866
查看次数

标签 统计

android ×1

android-fragments ×1