Skip to content

Commit e28bc4c

Browse files
authored
Merge pull request #657 from Andr3Carvalh0/fix/crash_on_configuration_change
Fix a crash when a configuration change would happen (ie: rotate the device)
2 parents 6391f79 + e1738e7 commit e28bc4c

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

touchview/src/main/java/com/ortiz/touchview/TouchImageView.kt

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.content.res.Configuration
55
import android.graphics.*
66
import android.graphics.drawable.Drawable
77
import android.net.Uri
8-
import android.os.Build
98
import android.os.Bundle
109
import android.os.Parcelable
1110
import android.util.AttributeSet
@@ -20,6 +19,8 @@ import android.view.animation.AccelerateDecelerateInterpolator
2019
import android.view.animation.LinearInterpolator
2120
import android.widget.OverScroller
2221
import androidx.appcompat.widget.AppCompatImageView
22+
import androidx.core.os.BundleCompat
23+
import androidx.core.os.bundleOf
2324
import kotlin.math.abs
2425
import kotlin.math.max
2526
import kotlin.math.min
@@ -226,25 +227,27 @@ open class TouchImageView @JvmOverloads constructor(context: Context, attrs: Att
226227
}
227228
}
228229

229-
public override fun onSaveInstanceState(): Parcelable? {
230-
super.onSaveInstanceState()
231-
val bundle = Bundle()
232-
bundle.putInt("orientation", orientation)
233-
bundle.putFloat("saveScale", currentZoom)
234-
bundle.putFloat("matchViewHeight", matchViewHeight)
235-
bundle.putFloat("matchViewWidth", matchViewWidth)
236-
bundle.putInt("viewWidth", viewWidth)
237-
bundle.putInt("viewHeight", viewHeight)
230+
public override fun onSaveInstanceState(): Parcelable {
238231
touchMatrix.getValues(floatMatrix)
239-
bundle.putFloatArray("matrix", floatMatrix)
240-
bundle.putBoolean("imageRendered", imageRenderedAtLeastOnce)
241-
bundle.putSerializable("viewSizeChangeFixedPixel", viewSizeChangeFixedPixel)
242-
bundle.putSerializable("orientationChangeFixedPixel", orientationChangeFixedPixel)
243-
return bundle
232+
233+
return bundleOf(
234+
"parent" to super.onSaveInstanceState(),
235+
"orientation" to orientation,
236+
"saveScale" to currentZoom,
237+
"matchViewHeight" to matchViewHeight,
238+
"matchViewWidth" to matchViewWidth,
239+
"viewWidth" to viewWidth,
240+
"viewHeight" to viewHeight,
241+
"matrix" to floatMatrix,
242+
"imageRendered" to imageRenderedAtLeastOnce,
243+
"viewSizeChangeFixedPixel" to viewSizeChangeFixedPixel,
244+
"orientationChangeFixedPixel" to orientationChangeFixedPixel
245+
)
244246
}
245247

246248
public override fun onRestoreInstanceState(state: Parcelable) {
247249
if (state is Bundle) {
250+
super.onRestoreInstanceState(BundleCompat.getParcelable(state, "parent", Parcelable::class.java))
248251
currentZoom = state.getFloat("saveScale")
249252
floatMatrix = state.getFloatArray("matrix")!!
250253
prevMatrix.setValues(floatMatrix)
@@ -253,15 +256,8 @@ open class TouchImageView @JvmOverloads constructor(context: Context, attrs: Att
253256
prevViewHeight = state.getInt("viewHeight")
254257
prevViewWidth = state.getInt("viewWidth")
255258
imageRenderedAtLeastOnce = state.getBoolean("imageRendered")
256-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
257-
viewSizeChangeFixedPixel = state.getSerializable("viewSizeChangeFixedPixel", FixedPixel::class.java)
258-
orientationChangeFixedPixel = state.getSerializable("orientationChangeFixedPixel", FixedPixel::class.java)
259-
} else {
260-
@Suppress("DEPRECATION")
261-
viewSizeChangeFixedPixel = state.getSerializable("viewSizeChangeFixedPixel") as FixedPixel?
262-
@Suppress("DEPRECATION")
263-
orientationChangeFixedPixel = state.getSerializable("orientationChangeFixedPixel") as FixedPixel?
264-
}
259+
viewSizeChangeFixedPixel = BundleCompat.getSerializable(state, "viewSizeChangeFixedPixel", FixedPixel::class.java)
260+
orientationChangeFixedPixel = BundleCompat.getSerializable(state, "orientationChangeFixedPixel", FixedPixel::class.java)
265261
val oldOrientation = state.getInt("orientation")
266262
if (orientation != oldOrientation) {
267263
orientationJustChanged = true

0 commit comments

Comments
 (0)