Getting same item position in RecyclerView, when click on back button

Save Instance Code:

Parcelable mListState;

protected void onSaveInstanceState(Bundle state) {
     super.onSaveInstanceState(state);

     // Save list state
     mListState = mLayoutManager.onSaveInstanceState();
     state.putParcelable(LIST_STATE_KEY, mListState);
}

Restore State Code:

protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);

    // Retrieve list state and list/item positions
    if(state != null)
        mListState = state.getParcelable(LIST_STATE_KEY);
}

Here in onResume() update the layout manager:

@Override
protected void onResume() {
    super.onResume();

    if (mListState != null) {
        mLayoutManager.onRestoreInstanceState(mListState);
    }
}

Hope this helps


The main problem is that onPause you are clearing the whole data and when the fragment getting visible again you are requesting to server from the page 1. Comment the following code.

    @Override
public void onPause(){
    super.onPause();
    //listSuperHeroes.clear();
    //adapter.notifyDataSetChanged();
    //requestCount1=1;
   }
  }

update the isLastPageLoaded here:

private boolean isLastPageUpdated = false;
     private JsonArrayRequest getDataFromServer(int requestCount) {
            final String DATA_URL = "https://www.XYZ.php?matri_id="+email+"&page=";
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL + String.valueOf(requestCount),
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            if (response.length() == 0) {
                                progressBar.setVisibility(View.GONE);
                                tvMSG.setText("There is no broader matches");
                    isLastPageLoaded = true;
                            }
                            else {
                                parseData(response);
                                tvMSG.setVisibility(View.GONE);
                                progressBar.setVisibility(View.GONE);
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            progressBar.setVisibility(View.GONE);
                            Toast.makeText(getActivity(), "No More Items 
                            Available", Toast.LENGTH_SHORT).show();
                        }
                    });
            return jsonArrayRequest;
        }

and in getData()

 private void getData() {
if(!isLastPageUpdated){
    requestQueue.add(getDataFromServer(requestCount1));
    requestCount1++;
}}