迁移AndroidX,findFragmentById后(findFragmentByTag也)开始返回空值,当我试图获得片段。
片段在XML这样说:
<fragment
android:id="@+id/id_fragment"
android:tag="tag_fragment"
android:name="com.blaa.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
片段看起来是这样的:
class MyFragment : androidx.fragment.app.Fragment() {
...
}
和活动如下:
class MyActivity : AppCompatActivity(){
...
}
依赖关系:
...
implementation "androidx.appcompat:appcompat:1.0.2"
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "com.google.android.material:material:1.1.0-alpha03"
...
这里是我如何把它里面的代码:
supportFragmentManager.findFragmentById(R.id.id_fragment)
要么
supportFragmentManager.findFragmentByTag(R.id.tag_fragment)
这两个返回null。我也尝试使用刚刚fragmentManager,用空的结果,并因为它返回的简单片段它无论如何都不会工作,没有androidx之一。
感谢您的任何意见
索引访问,都fm.getFragments().get(0)
或fm.getFragments().get(1)
,可能会奏效。
如果它不...它可能无法找到它,因为它尚未加入(计时问题)。
fm.getFragments()
至少是有益的,看看有什么可以甚至在当前的时间点。