android-expr-eval
android-expr-eval copied to clipboard
Scrollable view changes state on device rotation
When I perform this sequence of events, the scrollable view on screen goes back to the bottom:
Precondition: The list of events is longer than the screen size
- Scroll Up the list of events
- Orientation Change
Thank you for reporting. I understand what you mean, and I reproduced the behavior you described in the emulator. I'll try to see if there is a way to keep the scrollable TextView at the same scrolling point after orientation change.
I've done a bit of research about this, but I haven't found a solution. The activity is not restarted after orientation change (orientation is included in android:configChanges) to preserve the context (variables, functions) without having to save it in a Bundle and then restore it (as far as I understood I'd have to make some classes implement Parcelable which would be a bit tedious, and this save/reload would probably impact performance too), so I just put orientation
in android:configChanges
.
To achieve what you ask for, I'd have to save the scrolling position before the orientation change and then restore it after the change. However there is only one method that is called on a configuration change, onConfigurationChanged
, and it's not specified if it's actually called before or after the change, so I have no idea when to save the position and then restore it.
I guess you are looking for onSaveInstanceState()
https://developer.android.com/guide/topics/resources/runtime-changes.html
I guess you are looking for onSaveInstanceState()
No. As I said before, I put orientation
in android:configChanges
to prevent Android from restarting the activity on orientation change, and thus losing the ExpressionContex. Correct me if I'm wrong, but as far as I understand onSaveInstanceState
and onRestoreInstanceState
are only called when the activity is destroyed and then recreated, i.e. orientation changes but android:configChanges
does not contain orientation
, so Android calls onSaveInstanceState
, destroys the activity, recreates it and calls onRestoreInstanceState
on the new instance.
On a configuration change specified in android:configChanges
, only onConfigurationChanged
is called, and I can't know if it's called before or after the view is "rotated" and scrolled to bottom.