问题
I have a BottomNavigationView
displaying Fragments
, in my first fragment I'm using volley to fetch JSON data and populate it in a RecyclerView
like below:
private void loadRecyclerViewData() {
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL_DATA,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
bar.setVisibility(View.GONE);
try {
JSONArray array = new JSONArray(s);
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
ListItem item = new ListItem(
o.getString("name"),
o.getString("bio"),
o.getString("imageurl")
);
listItems.add(item);
}
adapter = new MyAdapter(listItems, getContext());
rv.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
bar.setVisibility(View.GONE);
Toast.makeText(getContext(), volleyError.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
The above gets called in my fragments onCreateView
.
It gets displayed like I want it to, but, this is where the issue is. When I go to another fragment and return (by using the BottomNavigationView
), then onCreateView
will be called again and the data will load again.
a Good example of this is the YouTube and Instagram app, where the data doesn't get reloaded every time when switching between views.
My Question:
How can I navigate between fragments using BottomNavigationView
without calling onDestroyView
, or is there another way to avoid the issue I' having?
Here is a similar question.
EDIT 1 : Adding more context to the question
When selecting a item inside BottomNavigationView
it inflates/replaces a fragment, causing onCreateView
to be called. My method is being called within onCreateView
, this means that every time I "switch" between fragments, my method will be called again causing Volley to fetch the data again.
回答1:
UPDATE:
If you are using replace fragments when you click the BottomNavigationView items, the onCreateView method always will be called after replace your Fragment, this method is automatic called according to lifecycle of the Fragment
.
You can use Non-Swipeable viewPager as the structure mentioned below or you can try to use save instance in Bundles. You can set up a logic like that: If your savedState is null, make request and save the data, if is not, use saved data to fill the list.
Source of Article
A Useful answer
I think your problem is related with viewPager
.
Try this:
viewPager.setOffscreenPageLimit(3);
In this example, I set 3 as the offscreen page limit. The Default value is 1.
The value 1 means, your viewPager
only creates 1 from left side, 1 from right side of your current page. Others will be destroyed and recreated when you select fragment in the range limit.
For example, I say, your offscreen page limit is 1 and you have 5 pages in the viewPager. At start, you are at the first page. 1st and 2nd pages are created, others aren't. If you select the 3rd page, 4th page will be recreated and 1st will be destroyed. The number means, you have only X number neighbours can be already created, others will be destroyed. If you increase the offscreen page limit, you can make all the fragments active then, there will be no recreation issue that bothers you.
Source Link
来源:https://stackoverflow.com/questions/50073909/load-data-only-once-when-using-bottomnavigationview