getParentFragment API 16

后端 未结 3 1671
小蘑菇
小蘑菇 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:48

    To get the parent fragment in older (and newer) versions, I found a way around:

    1) Set the tag of the ParentFragment in your activity (via .add() or .replace()). Check the link below for more info, or step 2 for a similar example in the ParentFragment : http://developer.android.com/reference/android/app/FragmentTransaction.html#add(android.app.Fragment, java.lang.String)

    2) In the ParentFragment, collect the tag (using 'this'), and add it to the newInstance() of your ChildFragment:

      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    
      // Get the fragment, if you want to re-use it
      ChildFragment fragment = (ChildFragment) fragmentManager.findFragmentByTag();
    
      // Create a new fragment if it doesn't already exist.
      if (fragment == null)
      {
         // Collect the tag of your ParentFragment and add it to the newInstance() of the ChildFragment
         String parentTag = this.getTag();
         fragment = ChildFragment.newInstance(parentTag);
      }
      // Put the fragment in the .replace() or .add() of the transaction.
      // You might use a childTag as well, but it's not necessary for solving your problem
      fragmentTransaction.replace(R.id.my_fragment_view, fragment, childTag);
      fragmentTransaction.commit();
    

    3) Make sure to save the tag in the arguments of the ChildFragment. Collect the tag from the arguments in onAttach(), and collect the ParentFragment through the 'activity' parameter from onAttach():

    private static final String PARENT_TAG = "parent_tag";
    ParentFragment parentFragment;
    
    public static ChildFragment newInstance(String parentTag)
    {
       ChildFragment fragment = new ChildFragment();
       Bundle args = new Bundle();
       args.putString(PARENT_TAG, parentTag);
       fragment.setArguments(args);
       return fragment;
    }
    
    @Override
    public void onAttach(Activity activity)
    {
       super.onAttach(activity);
    
       // Collect the tag from the arguments
       String tag = getArguments().getString(PARENT_TAG);
    
       // Use the tag to get the parentFragment from the activity, which is (conveniently) available in onAttach()
       parentFragment = (ParentFragment) activity.getFragmentManager().findFragmentByTag(tag);
    }
    

    4) Now you've got your ParentFragment inside your ChildFragment and you can use it whenever you need it, so where you used this:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
    {
         parentFragment = (ParentFragment) getParentFragment();
    }
    else
    {
         parentFragment = ParentFragment.StaticThis;
    }
    

    you can now:

    parentFragment.justDoSomethingCoolWithIt(); // and prevent memory leaks through StaticThis ;-)
    

提交回复
热议问题