Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

added BundleExtensions for bundle.getParcelable() and bundle.getParcelableArrayList() methods + a bit of refactoring #689

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions conductor/src/main/java/com/bluelinelabs/conductor/Backstack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<RouterTransaction> {

private val backstack: Deque<RouterTransaction> = ArrayDeque()
Expand Down Expand Up @@ -81,12 +83,10 @@ internal class Backstack : Iterable<RouterTransaction> {
}

fun restoreInstanceState(savedInstanceState: Bundle) {
val entryBundles = savedInstanceState.getParcelableArrayList<Bundle?>(KEY_ENTRIES)
if (entryBundles != null) {
entryBundles.reverse()
for (transactionBundle in entryBundles) {
backstack.push(RouterTransaction(transactionBundle!!))
}
val entryBundles = savedInstanceState.parcelableArrayListCompat<Bundle>(KEY_ENTRIES)

entryBundles?.reversed()?.forEach { bundle ->
backstack.push(RouterTransaction(bundle))
}

onBackstackUpdatedListener?.onBackstackUpdated()
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <reified T : Parcelable> Bundle.parcelableArrayListCompat(key: String) : ArrayList<T>? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelableArrayList(key, T::class.java)
} else {
getParcelableArrayList(key)
}
}

@Suppress("DEPRECATION")
inline fun <reified T : Parcelable> Bundle.parcelableCompat(key: String) : T? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelable(key, T::class.java)
} else {
getParcelable(key)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,13 @@ private interface LifecycleHandlerDelegate : ActivityLifecycleCallbacks {

fun handleOnCreate(savedInstanceState: Bundle?) {
savedInstanceState ?: return
data.permissionRequestMap = savedInstanceState.getParcelable<StringSparseArrayParceler>(KEY_PERMISSION_REQUEST_CODES)
data.permissionRequestMap = savedInstanceState.parcelableCompat<StringSparseArrayParceler>(KEY_PERMISSION_REQUEST_CODES)
?.stringSparseArray
?: SparseArray()
data.activityRequestMap = savedInstanceState.getParcelable<StringSparseArrayParceler>(KEY_ACTIVITY_REQUEST_CODES)
data.activityRequestMap = savedInstanceState.parcelableCompat<StringSparseArrayParceler>(KEY_ACTIVITY_REQUEST_CODES)
?.stringSparseArray
?: SparseArray()
data.pendingPermissionRequests = savedInstanceState.getParcelableArrayList(KEY_PENDING_PERMISSION_REQUESTS)
data.pendingPermissionRequests = savedInstanceState.parcelableArrayListCompat(KEY_PENDING_PERMISSION_REQUESTS)
?: ArrayList()
}

Expand Down