getParentFragment API 16

后端 未结 3 1672
小蘑菇
小蘑菇 2021-01-04 10:59

We all know getParentFragment of Fragment is introduced in API 17.

So what if we want to get parent fragment in API 16 and below (Consideri

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-04 11:38

    Based on your comment if you want to talk back from the "items" in your ViewPager (I'm guessing this is a Fragment) to the container of the ViewPager which is a FragmentActivity you can use an interface.

    (1) Either declar the interface in the Fragment itself or as a separate file (2) "Initialize" the inteface in your fragment's onAttach method. For example

     private SomeInterface blah;
    
     @Override
     public void onAttach(Activity activity){
        blah = (SomeInterface) activity;
     }
    

    (3) Implement the interface in your FragmentActivity.

    You can then callback to the FragmentActivity from your Fragment. From there you can call any method you want within the FragmentActivity or, if you get a reference to any of the other fragments that are loaded into your ViewPager, call any public method within that Fragment. This allows you to communicate between fragments and their container without a memory leak.

提交回复
热议问题