如果我向 Activity
A
添加一个 Fragment
实例,如下所示...
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_container, myFragment, "MY_FRAGMENT_TAG")
.commit();
...然后我在 Activity
A
之上启动 Activity
B
。是否可以(?)获取 Activity
B
中先前提交的 Fragment
实例的句柄,如下所示...
getSupportFragmentManager()
.findFragmentByTag("MY_FRAGMENT_TAG");
...这会返回null
,所以我想知道是否可以使用上述方法或其他方法?
最佳答案
这是不可能的,你只能在其FragmentActivity中使用Fragments。
在 SDK 文档中,它说 fragment 是应用程序用户界面或行为的一部分,可以放置在 Activity 中。 Fragment 与它所在的 Activity 紧密相关,不能单独使用。 所以,你不能。
请阅读引用文档:https://developer.android.com/reference/android/app/Fragment.html
关于android - 可以在多个 Activity 中重复使用 Fragment 实例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973392/