android – 在Activity中获取Fragment实例
作者:互联网
I have added Fragment to Activity like
getSupportFragmentManager().beginTransaction()
.add(R.id.container,new MyFragment).commit();
其中container是FrameLayout的id
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
现在我怎样才能在Activity like this中获得Fragment的实例
从Fragment B获得结果后,我必须调用Fragment A的方法.
我在Fragment B中创建了一个接口,并在Activity.Now中实现了它.我必须将结果传递给Fragment A.我无法获得Fragment A的实例.
One thing i don’t wanna do is to create a private instance of Fragment A in Activity and call it’s method.
解决方法:
试试这个
getSupportFragmentManager().beginTransaction()
.add(R.id.container,new MyFragment(),"MyFragment").commit();
获取片段
MyFragment frag = ((MyFragment) getSupportFragmentManager().findFragmentByTag("MyFragment"));
标签:android,interface,android-fragments,android-framelayout,fragmentmanager 来源: https://codeday.me/bug/20190713/1449213.html