Android:如何将 Parcelable 对象传递给意图并使用包的 getParcelable 方法? [英] Android: How to pass Parcelable object to intent and use getParcelable method of bundle?
问题描述
为什么bundle有getParcelableArrayList
、getParcelable
方法;但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屋!