在 Android 开发中,Bundle 是一个常用的数据容器,用于在组件之间(如 Activity 之间)传递数据。传统上,我们使用 Bundle.GetParcelable(string key) 方法来检索存储的 Parcelable 对象。然而,从 Android API 33 (Tiramisu) 开始,此方法已被标记为废弃(@Deprecated),并建议使用新的、类型更安全的 GetParcelable(String key, Class
原有的 GetParcelable(string key) 方法签名如下(摘自 Android 源码):
/* @deprecated Use the type-safer {@link #getParcelable(String, Class)} starting from Android * {@link Build.VERSION_CODES#TIRAMISU}. */ @Deprecated @Nullable public <T extends Parcelable> T getParcelable(@Nullable String key) { // Implementation here }
其主要问题在于它返回一个泛型的 T 类型,但没有在方法签名中强制指定 T 的具体类型,从而可能导致运行时类型转换错误。为了提高类型安全性并减少潜在的运行时异常,新的 API 引入了 Class
新的 GetParcelable(String key, Class
/** * Returns the value associated with the given key or {@code null} if: * <ul> * <li>No mapping of the desired type exists for the given key. * <li>A {@code null} value is explicitly associated with the key. * <li>The object is not of type {@code clazz}. * </ul> * * @param key a String, or {@code null} * @param clazz The type of the object expected * @return a Parcelable value, or {@code null} */ @SuppressWarnings("unchecked") @Nullable public <T> T getParcelable(@Nullable String key, @NonNull Class<T> clazz) { // Implementation here }
这个新方法要求调用者显式提供预期的类型信息,从而在编译时就能进行更严格的类型检查,避免了不必要的运行时错误。
在 Xamarin.Android 项目中,我们通常会定义一个 C# 类并使其实现 IParcelable 接口(或通过 [Parcelable] 属性自动生成相关代码),以便在 Bundle 中传递。
原始(已废弃)的代码示例:
假设我们有一个名为 User 的 C# 类,它实现了 IParcelable 接口,并且我们通过 Intent 和 Bundle 在活动之间传递 User 对象。
发送 User 对象:
// 假设 MyUser 是一个已填充数据的 User 实例 User MyUser = new User("...", "...", /* ...其他属性... */); Intent intent = new Intent(this, typeof(Menu)); Bundle bundlee = new Bundle(); bundlee.PutParcelable("MyUser", MyUser); // 将 User 实例存入 Bundle intent.PutExtra("TheBundle", bundlee); StartActivity(intent);
请注意,PutParcelable 方法并未废弃,其使用方式保持不变。
接收 User 对象(旧的、已废弃的方式):
// 在目标 Activity 中接收 Bundle Bundle bundlee = Intent.GetBundleExtra("TheBundle"); User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""); // 初始化,但这行在实际使用中通常不是必需的,可以直接赋值 MyUser = bundlee.GetParcelable("MyUser") as User; // 废弃警告出现在这里
上述代码中的 bundlee.GetParcelable("MyUser") 会触发废弃警告,因为它使用的是不带 Class 参数的旧方法。
要解决 Bundle.GetParcelable 的废弃问题,我们需要使用新的 GetParcelable(String key, Class
Java.Lang.Class.FromType() 方法能够将一个 .NET System.Type 对象转换为对应的 Java java.lang.Class 对象,这正是 GetParcelable 新方法所需要的。
接收 User 对象(新的、推荐的方式):
// 在目标 Activity 中接收 Bundle Bundle bundlee = Intent.GetBundleExtra("TheBundle"); if (bundlee != null) { // 使用新的 GetParcelable 方法,通过 Java.Lang.Class.FromType 提供类型信息 // MyUser = bundlee.GetParcelable("MyUser", Java.Lang.Class.FromType(typeof(User))) as User; // 或者,如果 MyUser 实例已经存在(如通过默认构造函数创建),也可以使用其类型 // User MyUser = new User(); // 假设 User 有一个无参构造函数 // MyUser = bundlee.GetParcelable("MyUser", Java.Lang.Class.FromType(MyUser.GetType())) as User; // 更简洁和推荐的做法是直接获取并赋值 User MyUser = bundlee.GetParcelable("MyUser", Java.Lang.Class.FromType(typeof(User))) as User; if (MyUser != null) { // 成功获取到 MyUser 对象,进行后续操作 Console.WriteLine($"User Name: {MyUser.Name}"); // 假设 User 类有 Name 属性 } else { Console.WriteLine("Failed to retrieve MyUser object or it was null."); } } else { Console.WriteLine("Bundle is null."); }
在上述代码中,Java.Lang.Class.FromType(typeof(User)) 将 C# 的 User 类型转换为 Java 运行时所需的 Class 对象。这样,GetParcelable 方法就能确保返回的对象是 User 类型或其子类型,从而满足类型安全的要求。
通过上述迁移步骤,您的 Xamarin.Android 应用将能够兼容 Android API 33 及更高版本,并消除 Bundle.GetParcelable 相关的废弃警告,提升代码的质量和前瞻性。
以上就是Xamarin Android API 33+ 中 Bundle.GetParcelable 废弃问题的解决方案与类型安全迁移指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号