The_Coder
The_Coder

Reputation: 61

Api calls from a fragment using Volley

I am making API calls from my fragment and getting the response using volley. The API calls are made again every time I click on that fragment tab. I want the API call to take place only once. Is there any way to achieve this? I tried searching for the solution but did not find anything useful. Below is the code of my fragment.

public class Tab3News extends Fragment {

    private RecyclerView newsView;
    private NewsAdapter newsadapter;
    String myxmlResponse;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("Making request again","hello");
        View layout = inflater.inflate(R.layout.tab3news, container, false);
        newsView = (RecyclerView) layout.findViewById(R.id.newstable);
        String symPassed = ((SendString)getActivity()).message;

        String XmlURL = "http://demoapplication-env.us-east-2.elasticbeanstalk.com/?symbol="+symPassed+"&indicator=XML";


        RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());

        StringRequest req = new StringRequest(Request.Method.GET, XmlURL,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //processData(response);

                            myxmlResponse = response;

                            newsView.setHasFixedSize(true);
                            //newsView.setItemAnimator(new DefaultItemAnimator());
                            newsView.setLayoutManager(new LinearLayoutManager(getActivity()));
                            newsView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
                            newsadapter = new NewsAdapter(getActivity(),getData());
                            newsView.setAdapter(newsadapter);

                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        } catch (XmlPullParserException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // handle error response
                    }
                }
        );
        queue.add(req);


        return layout;
    }
}

Upvotes: 0

Views: 977

Answers (2)

p.mathew13
p.mathew13

Reputation: 921

I think it is better to call the api in activity and pass the arguments through bundle rather than using a flag in shared preference. Then again, its my personal opinion.

Upvotes: 0

amit singh
amit singh

Reputation: 1422

There are two ways to do this-

1- Call the API from the parent activity of this fragment and accordingly pass the data to the fragment using 'setArguments(bundle)', by doing this, your API would not get called everytime on loading of the fragment.

2- Keep a boolean value in the Preference whenever the API is called-

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
        mMyPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isFirstTime = mMyPrefs.getBoolean("IS_FIRST_TIME", true);
       if (mIsFirstTime)
                    {
                        SharedPreferences.Editor editPrefs = mMyPrefs.edit();
                        editPrefs.putBoolean("IS_FIRST_TIME", false);
                        editPrefs.apply();

                        callAPI();
                    }
      else
      {
             //TO DO YOUR STUFF
      }
}

Upvotes: 1

Related Questions