Android:如何将 Parcelable 对象传递给意图并使用包的 getParcelable 方法? [英] Android: How to pass Parcelable object to intent and use getParcelable method of bundle?

查看:22
本文介绍了Android:如何将 Parcelable 对象传递给意图并使用包的 getParcelable 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么bundle有getParcelableArrayListgetParcelable方法;但Intent 只有 putParcelableArrayListExtra 方法?我可以只传输一个元素的 object 而不是 ArrayList 吗?那么,getParcelable 是干什么用的?

Why bundle has getParcelableArrayList, getParcelable methods; but Intent has only putParcelableArrayListExtra method? Can I transmit only object<T>, not ArrayList of one element? Then, what is getParcelable for?

推荐答案

Intent 提供了一堆重载 putExtra() 方法.

Intent provides bunch of overloading putExtra() methods.

假设你有一个类 Foo 正确地实现了 Parcelable,将它放入一个活动的 Intent 中:

Suppose you have a class Foo implements Parcelable properly, to put it into Intent in an Activity:

Intent intent = new Intent(getBaseContext(), NextActivity.class);
Foo foo = new Foo();
intent.putExtra("foo ", foo);
startActivity(intent);

从另一个活动的意图中获取它:

To get it from intent in another activity:

Foo foo = getIntent().getExtras().getParcelable("foo");

希望这会有所帮助.

这篇关于Android:如何将 Parcelable 对象传递给意图并使用包的 getParcelable 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆