getParentFragment API 16

时间:2013-12-04 13:11:02

标签: android android-fragments

我们都知道API {17}中引入了getParentFragment Fragment

那么如果我们想要在API 16及更低版本中获取父片段呢(考虑到我使用支持Fragment的本地FragmentStatePagerAdapter并且嵌套片段没有问题)

有没有比我更好的方法?

在父母:

public class ParentFragment extends Fragment {

public static ParentFragment StaticThis;
...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

StaticThis = this;

...
}

在孩子:

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

3 个答案:

答案 0 :(得分:5)

根据您的评论,如果您想从ViewPager(我猜这是一个Fragment)中的“项目”回复ViewPager的容器,是FragmentActivity您可以使用界面。

(1)在Fragment本身中声明接口或作为单独的文件声明 (2)“初始化”片段的onAttach方法中的接口。例如

 private SomeInterface blah;

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

(3)在FragmentActivity中实施界面。

然后,您可以从FragmentActivity回复Fragment。在那里,您可以在FragmentActivity内调用您想要的任何方法,或者,如果您获得了加载到ViewPager中的任何其他片段的引用,请调用Fragment中的任何公共方法}。这允许您在片段和容器之间进行通信而不会发生内存泄漏。

答案 1 :(得分:4)

为了获得较旧(和较新)版本的父片段,我找到了解决办法:

1)在您的活动中设置ParentFragment的标签(通过.add()或.replace())。请查看下面的链接以获取更多信息,或者在ParentFragment中查看类似示例的步骤2:http://developer.android.com/reference/android/app/FragmentTransaction.html#add(android.app.Fragment,java.lang.String)

2)在ParentFragment中,收集标记(使用' this'),并将其添加到ChildFragment的newInstance()中:

  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)确保将标记保存在ChildFragment的参数中。从onAttach()中的参数中收集标记,并通过' activity'收集ParentFragment。来自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)现在你已经在你的ChildFragment中找到了你的ParentFragment,你可以随时使用它,所以你在哪里使用它:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
     parentFragment = (ParentFragment) getParentFragment();
}
else
{
     parentFragment = ParentFragment.StaticThis;
}
你现在可以:

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

答案 2 :(得分:4)