From 39553a1b848ee573acb306e3297688df096f404f Mon Sep 17 00:00:00 2001 From: mrenouf Date: Sun, 3 Mar 2024 12:10:02 -0500 Subject: Fix CreationExtras.addDefaultArgs when args absent addDefaultArgs grabs the existing Bundle from CreationExtras and adds in the additional values. If default args aren't present (whenever the initial intent did not have extras), the updated bundle was never returned within the original instance since it was relying on modifying the Bundle in-place. This corrects the issue by explicitly returning a new instance containing the updated `DEFAULT_ARGS` Bundle. Test: atest com.android.intentresolver.v2.ext.CreationExtrasExtTest Change-Id: I5a7c61baaaeefa2878b4041d809c348bf5ac70c0 --- .../com/android/intentresolver/v2/ext/CreationExtrasExt.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'java/src') diff --git a/java/src/com/android/intentresolver/v2/ext/CreationExtrasExt.kt b/java/src/com/android/intentresolver/v2/ext/CreationExtrasExt.kt index ebd613f1..6c36e6aa 100644 --- a/java/src/com/android/intentresolver/v2/ext/CreationExtrasExt.kt +++ b/java/src/com/android/intentresolver/v2/ext/CreationExtrasExt.kt @@ -18,14 +18,17 @@ package com.android.intentresolver.v2.ext import android.os.Bundle import android.os.Parcelable +import androidx.core.os.bundleOf import androidx.lifecycle.DEFAULT_ARGS_KEY import androidx.lifecycle.viewmodel.CreationExtras +import androidx.lifecycle.viewmodel.MutableCreationExtras -/** Adds one or more key-value pairs to the default Args bundle in this extras instance. */ +/** + * Returns a new instance with additional [values] added to the existing default args Bundle (if + * present), otherwise adds a new entry with a copy of this bundle. + */ fun CreationExtras.addDefaultArgs(vararg values: Pair): CreationExtras { val defaultArgs: Bundle = get(DEFAULT_ARGS_KEY) ?: Bundle() - for ((key, value) in values) { - defaultArgs.putParcelable(key, value) - } - return this + defaultArgs.putAll(bundleOf(*values)) + return MutableCreationExtras(this).apply { set(DEFAULT_ARGS_KEY, defaultArgs) } } -- cgit v1.2.3-59-g8ed1b