FragmentPagerAdapter的第二个片段中的访问受限 [英] Limited access in 2nd fragment of FragmentPagerAdapter

查看:73
本文介绍了FragmentPagerAdapter的第二个片段中的访问受限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类似的 onViewCreated 结构片段

I have two similar by onViewCreated structure fragments

//1st
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    textDebug1 = getActivity().findViewById(R.id.view_debug1); //from layout1
    textDebug2 = getActivity().findViewById(R.id.view_debug2); //from layout2
//2nd
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    textDebug1 = getActivity().findViewById(R.id.view_debug1); //<--gives  error; layout1
    textDebug2 = getActivity().findViewById(R.id.view_debug2); //from layout2

在我的onCreateView中,

我有相同的行但布局不同.

in onCreateView for both I have same line but different layouts.

     View rootView = inflater.inflate(R.layout.activity_frag_main, container, false);

TextView 位于不同的布局上,如果我选择加载第一个片段,则代码运行良好,但是如果我选择第2个运行第一个片段,则 getActivity()返回 null .如果两者的创建都相同,可能会导致什么?

TextViews are located on different layouts and if I choose to load 1st fragment then code works well, but if I choose 2nd to run 1st then getActivity() returns null. What might cause this if creation is identical for both?

推荐答案

如果您查看Fragment生命周期,您会发现有一个 onAttach()和一个 onDetach()方法.他们指的是碎片的状态.由于您使用的是ViewPager,因此您的片段可能会被分离,并且 getActivity()可能会返回 null .

If you look at the Fragment lifecycle, you'll see that there's an onAttach() and an onDetach() methods. They refer to the Fragment's state. Since you're using a ViewPager, your fragments might be detached and the getActivity() might return null.

如本文所述,这是ViewPager中的片段发生的情况:

As stated on this article, this is what happens with a Fragment inside a ViewPager:

当页面不再可见或与可见页面相邻时,ViewPager要求适配器销毁它.但是,那FragmentPagerAdapter不会完全破坏片段.它呼吁 FragmentTransaction.detach(fragment),它会破坏片段的整个视图层次结构,而不是对象.下次ViewPager希望该页面可以检索相同的片段对象,并且视图被重建.在此过程中,再次调用onCreateView(),这是初始化视图的逻辑所属的地方.

When the page is no longer visible or adjacent to the visible page the ViewPager asks the adapter to destroy it. However the FragmentPagerAdapter doesn't destroy the fragment entirely. It calls FragmentTransaction.detach(fragment), which destroys the fragment's entire view hierarchy, but not the object. Next time the ViewPager wants that page the same fragment object can be retrieved and the view is rebuilt. In the process the onCreateView() is called again, this is where your logic to initialise the view belongs.

并且如文档中所述 :

警告:如果您在Fragment中需要一个Context对象,则可以调用getContext().但是,只有在将片段附加到活动时,才应小心调用getContext().当片段尚未附加或在其生命周期结束时分离时,getContext()将返回null .

您可以尝试的一件事是保留片段实例.在您片段的 onCreate()中,尝试调用方法 setRetainInstance(true);

One thing you can try is to retain the fragment instance. Inside your Fragment's onCreate(), try to call the method setRetainInstance(true);

也请检查此答案

Also, check this answer and this other one here on SO, it might be useful for your use case.

这篇关于FragmentPagerAdapter的第二个片段中的访问受限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆