我已经升级了 targetsdkversion
和 compilesdkversion
33
。
我现在要警告告诉我 and 方法被弃用。
我从类,这两种方法确实被弃用。
该文档表明我使用新的 getSerializableExtra(name:clazz :) and getParcelableExtra(name:clazz :) 方法。
谁能帮助我使用新方法?
示例警告
- 警告
对象
:
-
i.sstatic.net/j4zvy.png“ rel =“ noreferrer”>
I have upgraded targetSdkVersion
and compileSdkVersion
to 33
.
I am now getting warnings telling me that the getSerializableExtra(name:) and getParcelableExtra(name:) methods are deprecated.
I checked and confirmed from the documentation in the Intent class that these two methods are indeed deprecated.
The documentation suggests that I use the new getSerializableExtra(name:clazz:) and getParcelableExtra(name:clazz:) methods instead.
Can anyone can help me use the new methods?
Example warnings
- Warning while getting an
Object
:

- Warning while getting a
List
or ArrayList
:

发布评论
评论(12)
这就是我使用的:
我也为 getParcelcelable 在这里要求添加到直接支持库
This is what I use:
I've also written the same for getParcelable here and requested this to be added to the Support libraries directly
方法
t getSerializableExtra(string,class< t>)
来自Android 33。因此,您将使用IF块用于使用Android以下33以下的设备。
然后您可以在活动中使用它:
method
T getSerializableExtra(String, Class<T>)
is introduced from android 33.so you shoud use an if block for devices using android below 33.
and then you can use it in your activity like this:
对于我们仍然使用Java的人,此功能可以解决:
For those of us who still use Java, this function does the trick:
但是, Mohamad Seyedi的答案做到了。但是,从
onActivityResult()
获得意图数据的情况下,这将失败。我们无法在
onActivityResult()
内使用活动的意图获取数据。以下是我得到的解决方案。解决方案1:使用扩展功能:
用法:
解决方案2:使用常规函数:
用法:
However, Mohamad Seyedi's answer did the job. But it fails in the case of getting intent data from
onActivityResult()
.We can't get data using Activity's intent inside
onActivityResult()
. Below are the solutions I got.Solution 1: Using extension function:
Usage:
Solution 2: Using regular function:
Usage:
扩展 rumit patel的答案,使用带有Refifif类型参数的内联函数,用法更简单。
请注意,我认为返回类型可取消使用,因为
!!
应该只用作最后一个度假胜地(或在来电侧)。用法:
Expanding upon Rumit Patel's answer, using an inline function with reified type parameter, usage is even simpler.
Note that I made the return type nullable, because
!!
should in my opinion only be used as a last resort (or at caller side).Usage:
受穆罕默德(Mohamad)的回答的启发,您可以进行扩展名
,并在这样的地方称呼它:
Inspired by Mohamad's answer, you can do an extension
and call it everywhere like this:
直到自己还没有使用它。
正如文档所说,更新的方法为
t geterializableExtra(string,class&lt; t&gt;)
,因此您认为您可以按照以下方式使用它。Haven't used it myself till yet .
As the doc says the updated method is
T getSerializableExtra(String, Class<T>)
So you can use it as follows i think.然后,创建该功能
then, create that function
将回答以包裹的回答。
由于我们已经有了序列化的良好答案,因此如果您感到 bundlecomapt 和 intentCompat 版本, 您可以更长的时间创建这样的扩展名并使用
Since we already have good answers with Serializable, will answer in favour of Parcelable
If you feel BundleComapt and IntentCompat versions are longer you can create extensions like this and use
您可以使用顶级函数来序列化参数
,但是我遇到了一个问题,序列化对象列表
您可以使用这种方式放置和获取序列化参数数据,
序列化的数组列表
因为列表不是可序列化的类,您需要将其转换为支持这些支持这些 函数用于序列化
,此方式将其处理列出对象的序列化列表
,现在可以以这种方式序列化您的列表对象
You can use top functions to serialize arguments
but I had a problem serializing the List of object
You can use this way to put and get serialized argument data
Because List is not a Serializable Class, you need to convert it to Array List that supports serializable
These functions are used for serializable
and this way handle puts serialized List of objects
and now can get serialize of your list object in this way
对此有一个兼容性效果:
捆绑包并不可取,因此意图。首先需要检查extras。
There is a compat funtion for this:
bundle isn't nullable so intent.extras need to be null checked first.
bundlecompat
:它将行为,以便
bundle.getXyz(key ,t :: class.java)
在SDK 33及以下,此方法调用旧
bundle.getxyz(key)
,如果key
不存在或不匹配预期类型,null
将返回。There were compatibility functions added within
BundleCompat
:It will behave so that
Bundle.getXYZ(key, T::class.java)
Bundle.getXYZ(key)
and performs a checked castIf the
key
does not exist or does not match the expected type,null
is returned.