1

I have one recycler view, when clicking on the Recycler view item it goes to some other activity. Again if I come back from that activity to Main activity, I should show the same recycler view position. I cant use finish() , in this case. If i am using finish then I can able to be in the same recycler view position. But in my case i cant use that. After come back from second activity i need to refresh the api again to update the values, so if finish the activity i cant get the Api changes. And i tried to do by calling OnRestoresavedInstance state, but in that case I am facing null pointer exception. Is there any way to get back the same recycler view poistion when i am coming from second activity? i Used this Link to sort out my problem. But doesn't find any result.

please help me out of this. Thank you in advance.

Community
  • 1
  • 1
Yamuna
  • 157
  • 2
  • 15
  • You could store the index in SharedPreferences. Then, in the onResume method of the first activity, retrieve it. – Fustigador May 17 '17 at 08:18

1 Answers1

0

You can do this

First save the position of recycler view in shared prefernces

SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("SNOW_DENSITY",mSnowDensity);
editor.commit();

And then when you come back just fetch the value

SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
int snowDensity = settings.getInt("SNOW_DENSITY", 0); //0 is the default value

And now simple move to position in recycler view

 mRecyclerView.scrollToPosition(firstCounter - 1);

Hope this helps :)

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26