Android – get fragment instance in activity

getSupportFragmentManager().beginTransaction()
                    .add(R.id.container,new MyFragment).commit();

Where container is the ID of FrameLayout

 <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

Now how can I get an instance of fragment in activity like this

After getting the result from fragment B, I have to call the method of fragment a

I created an interface in fragment B and implemented it in activity. Now. I must pass the result to fragment A. I can't get an instance of fragment a

resolvent:

Try this

getSupportFragmentManager().beginTransaction()
                .add(R.id.container,new MyFragment(),"MyFragment").commit();

Get fragment

MyFragment frag = ((MyFragment) getSupportFragmentManager().findFragmentByTag("MyFragment"));

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>