在这种情况下,您可以尝试使用全局静态引用技术。
在 Application 类(扩展 Android 的 Application 类的类)中声明您的 childview。
public class AppController extends Application{
public static final String LOG_TAG = ApplicationController.class
.getSimpleName();
// Application Instance
private static ApplicationController applicationController = null;
public static ListView childview;
public static ChildAdapter childAdapter;
@Override
public void onCreate() {
super.onCreate();
applicationController = this;
}
}
现在在你的任何片段中假设片段A,检查childFragment的全局对象是否为null,如果它的null像其他chilfragment一样正常初始化它,因为它的引用是全局的,AppController中的变量现在包含childfragment的实例。
现在在 Fragment B 检查全局引用是否为 null,因为您在 Fragment A 中对其进行了初始化,所以您不会得到 null,现在将 childFragment 附加到 Fragment B 就像普通的 childfragments