How to prevent the data being loaded again when coming from another intent. For instance, I have a MainActivity
where I'm making GET call to APIs in onCreate()
method. I click on one of the cards and it fires an another activity called CardActivity
.
When I hit back from this CardActivity
, the data is being loaded again. I tried onPause()
method as well but not luck. How do I prevent the data which is being loaded when navigating between Activities.
Could anyone please guide me how to achieve this?
I'm using Volley library for making HTTP calls.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestQueue = MySingleton.getInstance(this.getApplicationContext()).getRequestQueue();
getRedditDefaultData();
getGoogleHeadlinesData();
}
private void getRedditDefaultData() {
final String url = "https://example.com"
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
...
dataset.add(new StaggeredCustomCard(
redditUserProfilePic,
redditUserName,
redditPostDateTime,
redditPostTitle,
null,
redditPostDescription));
}
if (dataset != null) {
staggeredGridAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
progressBar.setVisibility(View.GONE);
}
});
retryPolicy(jsonObjectRequest);
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}