简体   繁体   中英

Cannot retrieve View in onResume() method inside Fragment

I have a method in my fragment that I would like to call from onResume() . Unfortunately, my method requires that I pass it a View, which is defined in onCreateView() :

onResume():

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

        retrieveData(0, false, rootView);
    }

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)?

Can't move it to global scope because the inflater method parameter is part of the fragment interface.

You can only get the view from your fragment, not your activity. To do this, in your fragment 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

Edit : to get data from Activity, pass it in a Bundle . Doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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