From b138546e508473dde6613e9ee8ff37dad911ec7a Mon Sep 17 00:00:00 2001 From: evitwilly Date: Mon, 9 Oct 2023 20:37:01 +0700 Subject: [PATCH] added BundleExtensions for bundle.getParcelable() and bundle.getParcelableArrayList() methods + a bit of refactoring --- .../com/bluelinelabs/conductor/Backstack.kt | 12 +++++----- .../conductor/internal/BundleExtensions.kt | 23 +++++++++++++++++++ .../conductor/internal/LifecycleHandler.kt | 6 ++--- 3 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 conductor/src/main/java/com/bluelinelabs/conductor/internal/BundleExtensions.kt diff --git a/conductor/src/main/java/com/bluelinelabs/conductor/Backstack.kt b/conductor/src/main/java/com/bluelinelabs/conductor/Backstack.kt index 9d397499..4813f437 100644 --- a/conductor/src/main/java/com/bluelinelabs/conductor/Backstack.kt +++ b/conductor/src/main/java/com/bluelinelabs/conductor/Backstack.kt @@ -4,6 +4,8 @@ import android.os.Bundle import java.util.ArrayDeque import java.util.Deque +import com.bluelinelabs.conductor.internal.parcelableArrayListCompat + internal class Backstack : Iterable { private val backstack: Deque = ArrayDeque() @@ -81,12 +83,10 @@ internal class Backstack : Iterable { } fun restoreInstanceState(savedInstanceState: Bundle) { - val entryBundles = savedInstanceState.getParcelableArrayList(KEY_ENTRIES) - if (entryBundles != null) { - entryBundles.reverse() - for (transactionBundle in entryBundles) { - backstack.push(RouterTransaction(transactionBundle!!)) - } + val entryBundles = savedInstanceState.parcelableArrayListCompat(KEY_ENTRIES) + + entryBundles?.reversed()?.forEach { bundle -> + backstack.push(RouterTransaction(bundle)) } onBackstackUpdatedListener?.onBackstackUpdated() diff --git a/conductor/src/main/java/com/bluelinelabs/conductor/internal/BundleExtensions.kt b/conductor/src/main/java/com/bluelinelabs/conductor/internal/BundleExtensions.kt new file mode 100644 index 00000000..349ee10f --- /dev/null +++ b/conductor/src/main/java/com/bluelinelabs/conductor/internal/BundleExtensions.kt @@ -0,0 +1,23 @@ +package com.bluelinelabs.conductor.internal + +import android.os.Build +import android.os.Bundle +import android.os.Parcelable + +@Suppress("DEPRECATION") +inline fun Bundle.parcelableArrayListCompat(key: String) : ArrayList? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + getParcelableArrayList(key, T::class.java) + } else { + getParcelableArrayList(key) + } +} + +@Suppress("DEPRECATION") +inline fun Bundle.parcelableCompat(key: String) : T? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + getParcelable(key, T::class.java) + } else { + getParcelable(key) + } +} diff --git a/conductor/src/main/java/com/bluelinelabs/conductor/internal/LifecycleHandler.kt b/conductor/src/main/java/com/bluelinelabs/conductor/internal/LifecycleHandler.kt index cad60b4f..c8d6b9db 100644 --- a/conductor/src/main/java/com/bluelinelabs/conductor/internal/LifecycleHandler.kt +++ b/conductor/src/main/java/com/bluelinelabs/conductor/internal/LifecycleHandler.kt @@ -325,13 +325,13 @@ private interface LifecycleHandlerDelegate : ActivityLifecycleCallbacks { fun handleOnCreate(savedInstanceState: Bundle?) { savedInstanceState ?: return - data.permissionRequestMap = savedInstanceState.getParcelable(KEY_PERMISSION_REQUEST_CODES) + data.permissionRequestMap = savedInstanceState.parcelableCompat(KEY_PERMISSION_REQUEST_CODES) ?.stringSparseArray ?: SparseArray() - data.activityRequestMap = savedInstanceState.getParcelable(KEY_ACTIVITY_REQUEST_CODES) + data.activityRequestMap = savedInstanceState.parcelableCompat(KEY_ACTIVITY_REQUEST_CODES) ?.stringSparseArray ?: SparseArray() - data.pendingPermissionRequests = savedInstanceState.getParcelableArrayList(KEY_PENDING_PERMISSION_REQUESTS) + data.pendingPermissionRequests = savedInstanceState.parcelableArrayListCompat(KEY_PENDING_PERMISSION_REQUESTS) ?: ArrayList() }