airbnb / epoxy

Epoxy is an Android library for building complex screens in a RecyclerView
https://goo.gl/eIK82p
Apache License 2.0
8.5k stars 733 forks source link

Saving scroll position for fragment #385

Closed sauyee333 closed 6 years ago

sauyee333 commented 6 years ago

When using fragment, only onPaused is called instead of onSaveInstanceState. How can states like scroll position restored when back to the fragment? Thanks.

elihart commented 6 years ago

This is normal. On the backstack the fragment is paused, and then resumed when it is restored. The view is not destroyed in this case so state does not need to be restored.

If the device is rotated while the fragment is on the backstack then the view is destroyed and state is saved as expected.

This is just normal android fragment saved state behavior and not related to Epoxy.

sauyee333 commented 6 years ago

But the scroll position will get reset when resume from stack?

elihart commented 6 years ago

No, that's now how the fragment lifecycle works. If the view is not destroyed then nothing with its state should change unless you are manually doing some modification in one of the lifecycle callbacks (onResume, onPause, etc).

If you set up the recyclerview and epoxy controller in fragment#onActivityCreated (and then never touch it again) it should restore fine off the backstack with no extra configuration.

sauyee333 commented 6 years ago

Thanks for the info. But calling in fragment#onActivityCreate or fragment#onCreateView the same. The position in carousel (horizontal recyclerview) still get reset whenever coming back to fragment. Vertical recyclerview is ok. I'll try overwrite carousel and store position. Thanks.

elihart commented 6 years ago

@sauyee333 oh, you're talking about a nested recyclerview? that's very different...

You have to enable saved state for the model https://github.com/airbnb/epoxy/wiki/Saving-View-State

sauyee333 commented 6 years ago

Thanks for the info. Found the issue: we used replace fragment instead of add fragment. After changing to transaction.add fragment, the position remains.