|
|
爱看书的钥匙 · 数据处理-生成变量-SPSSPRO帮助中心· 3 月前 · |
|
|
挂过科的人字拖 · 喜讯︱我院博士生杨佳慧同学获2025年广东省 ...· 11 月前 · |
|
|
重情义的甘蔗 · 新疆伊斯兰教经学院院长的人大代表履职路-新华网· 1 年前 · |
|
|
有胆有识的自行车 · Python:五行代码实现两个视频画中画播放 ...· 1 年前 · |
|
|
老实的领结 · VSCODE ...· 2 年前 · |
我是通过意图传递数据与Parcelable和接收使用getParcelableExtra。然而,getParcelableExtra似乎是不推荐的,我如何修复这段代码中的弃用警告?另一种选择是,是否有其他选择?我使用的是compileSdkVersion 33。
代码片段:
var data = intent.getParcelableExtra("data")
发布于 2022-08-10 19:36:09
下面是我为
Bundle
&
Intent
使用的两种扩展方法
inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? = when {
SDK_INT >= 33 -> getParcelableExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T
inline fun <reified T : Parcelable> Bundle.parcelable(key: String): T? = when {
SDK_INT >= 33 -> getParcelable(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelable(key) as? T
}
我也是 请求将其添加到支持库中。
如果您需要ArrayList支持,则如下所示:
inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String): ArrayList<T>? = when {
SDK_INT >= 33 -> getParcelableArrayList(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableArrayList(key)
inline fun <reified T : Parcelable> Intent.parcelableArrayList(key: String): ArrayList<T>? = when {
SDK_INT >= 33 -> getParcelableArrayListExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableArrayListExtra(key)
}
发布于 2022-07-18 08:20:27
kotlin示例代码
val userData = if (VERSION.SDK_INT >= 33) {
intent.getParcelableExtra("DATA", User::class.java)
} else {
intent.getParcelableExtra<User>("DATA")
}
JAVA示例代码
if (android.os.Build.VERSION.SDK_INT >= 33) {
user = getIntent().getParcelableExtra("data", User.class);
} else {
user = getIntent().getParcelableExtra("data");
}
发布于 2022-07-18 08:53:37