I am using the PhotoView library in my Android project. The project contains the SaveStatePhotoView which is used to keep the state (zoom level, position) of the image view on configuration changes (rotation, ...).
// SaveStatePhotoView.java
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
final SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
restoreSavedState(ss);
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
The view works as desired on Android 7.1.1 and Android 9.
On Android 6.0.1 the state is lost: the image view resets to its initial state when the device is rotated.
I prepared a simple project to demonstrate the issue. Please note that I am using PhotoView 1.3.1 on purpose because I cannot include transitive androidx
dependencies at the moment.