Martin Erlic
Martin Erlic

Reputation: 5667

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.

Upvotes: 0

Views: 908

Answers (1)

J-Jamet
J-Jamet

Reputation: 857

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

Upvotes: 3

Related Questions