简体   繁体   English

无法在Fragment的onResume()方法中检索View

[英]Cannot retrieve View in onResume() method inside Fragment

I have a method in my fragment that I would like to call from onResume() . 我的片段中有一个方法,我想从onResume()调用。 Unfortunately, my method requires that I pass it a View, which is defined in onCreateView() : 不幸的是,我的方法要求我传递给它一个视图,该视图在onCreateView()定义:

onResume(): onResume():

    @Override
    public void onResume() {
        super.onResume();

        retrieveData(0, false, rootView);
    }

onCreateView(): onCreateView():

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Set view
        View rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
    ...
    }

Is there a way to call my retrieveData method as is in onResume() , or should I just create a new method without the rootView parameter (not ideal)? 是否可以像onResume()一样调用我的restoreData方法,还是应该只创建一个没有rootView参数的新方法(不理想)?

Can't move it to global scope because the inflater method parameter is part of the fragment interface. 无法将其移至全局范围,因为Inflater方法参数是片段接口的一部分。

You can only get the view from your fragment, not your activity. 您只能从片段中获取视图,而不能从活动中获取视图。 To do this, in your fragment onCreateView 为此,请在片段中onCreateView

save the root view as an Instance Variable like 将根视图另存为实例变量,例如

private View root;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_details_buddy, container, false);
... }

And call it in your onResume 并在您的onResume中调用它

Edit : to get data from Activity, pass it in a Bundle . 编辑:要从Activity获取数据,请将其传递到Bundle中 Doc 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM