0

I tried a lot of the possible solutions but they were not useful, simply i don't want to use onBackPressed to achieve this, i'm trying to get back to the previous Activity using Intent, i tried to pass the position with putExtra() and get position from the intent in the previous Activity and use recycleView.scrollToPosition(pos);, but the Activity is recreated and the scroll is not working, i also tried to use onSavedInstanceState() and onRestoreInstanceState but i got the same results, any one can give me an idea ? thanks in advance.

I also wondering why the method below is not working ?

@Override
protected void onPause() {
    super.onPause();
    lastFirstVisiblePosition = ((LinearLayoutManager) 
    recyclerView.getLayoutManager()).findLastVisibleItemPosition();
    Log.d("bePos", String.valueOf(lastFirstVisiblePosition));
}

@Override
protected void onResume() {
    super.onResume();
    Log.d("lastPos", String.valueOf(lastFirstVisiblePosition));
    recyclerView.scrollToPosition(lastFirstVisiblePosition);
}

Update I solved the problem of saving the last position but later i discovered that i can not scroll to that position in RecyclerView because i'm using pagination, RecyclerView just can not scroll to an item the adapter does not load yet, so how to solve such an issue ?

Wael Luay
  • 15
  • 1
  • 11
  • your logic is flawed, you are sending an intent to start an Activity, not simply bringing to the foreground an existing activity, the correct manner is using `onBackPressed()` you might try setting your first activity as `singleTask` in your manifest if you insist on sending a intent for an already running activity in the stack. remember `onNewIntent` will be called, I believe it will be `onNewIntent`, `onPause`, `onResume` - but don't quote me .. – Mark Aug 31 '17 at 00:28
  • Now the main problem isn't with saving the position but the RecyclerView is not scrolling to that position after recreating the Activity @MarkKeen – Wael Luay Aug 31 '17 at 00:50
  • if its "recreating" the activity, then its a different instance with only default initialised instance variable values .. – Mark Aug 31 '17 at 00:55
  • See the update please .. @MarkKeen – Wael Luay Aug 31 '17 at 13:03

2 Answers2

0

Save your last visible position in the onSaveInstanceState method or in a preference item, if you wish to retrieve it even after your app has been cleared from the running tasks (killed). I am showing both methods below:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //this is when your activity is not permanent
        outState.putInt("last_position", lastFirstVisiblePosition);
        //save permanently
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        preferences.edit().putInt("last_position", lastFirstVisiblePosition).apply();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        //retrieve your stored item
        lastFirstVisiblePosition = savedInstanceState.getInt("last_position");
        //retrieve any time except your first run
        //you can add these lines in the onResume if you used the preference method
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        //use 0 as default
        lastFirstVisiblePosition = preferences.getInt("last_position", 0);
    }

I prefer the preferences method for better persistence, even if your task is killed you can still retrieve it.

  • Now the position is saved, but the recyclerView is not scrolling to that postion and i don't know why is that ? – Wael Luay Aug 31 '17 at 00:48
  • Use `((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWIthOffset(lastFirstVisiblePosition, number_of_pixels);` Try it and give us results on the effect. There's quite a lot of information about that here: https://stackoverflow.com/questions/26875061/scroll-recyclerview-to-show-selected-item-on-top#26876044 – Clement Osei Tano Aug 31 '17 at 01:02
  • i used it before and the same issue, and i think this is happening because i i'm using pagination with fetching data from the server and not fetching all the data once, am i right with this ? – Wael Luay Aug 31 '17 at 01:08
  • It could be but I have not made the confirmation myself, if lastFirstVisiblePosition is not in your list then the results may be unpredictable. If you think pagination is the issue, then you can mitigate it by saving the 'page number' and 'line number' so you can fetch those when you want to display your list – Clement Osei Tano Aug 31 '17 at 01:12
  • It's true, the reason for that is the pagination, how can i solve this issue ? i tried to save the page number but this is not useful for me because i don't want to load the last page items, i want to scroll to the last item i was on before going to the next Activity, do you have an idea ? @Cool Guy CG – Wael Luay Aug 31 '17 at 12:41
  • I will go about it in several ways: First I may store an id of the particular item being displayed so I can fetch that, but to enhance smooth scrolling, I need to have the pages too, say I fetch 20 items per page, the page on which my 81st (assuming you got the right position to be 81) item will be is ((int)Math.ceil(81.0/20.0)). I then store the resulting page, 5 and position 81 in my preferences for use later. Next run, if I'm loading, I go straight to displaying page 5 and make provision for the other pages. You can post the pagination activity so we know how you achieve that. – Clement Osei Tano Sep 01 '17 at 10:51
0

This method helped me to get the last position. Just add this line of code in your RecyclerView inside of onBindViewHolder

holder.setIsRecyclable(false);
clauub
  • 1,134
  • 9
  • 19