Android Developer
Android Developer

Reputation: 125

Right Approach to Call Web Service(API) from Fragment in Android

I have an activity which has a fragment and a dialog. And I am calling an API from the fragment in the onCreateView method. And when I am on the fragment and opens the dialog then fragment again hits the API. I want to prevent it.I want that my app do not hit when I am opening a dialog.

// code

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_news, container, false);

        callApi()

         }

Upvotes: 1

Views: 2106

Answers (1)

umesh vashisth
umesh vashisth

Reputation: 359

call your api in onViewCreated like this.

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

           callApi();  //here call your function.


    }

Upvotes: 1

Related Questions