Android:如何将Parcelable对象传递给intent并使用bundle的getParcelable方法?

yit*_*al9 64 android bundle parcelable android-intent

为什么捆绑getParcelableArrayList,getParcelable方法; 但 Intent只有putParcelableArrayListExtra方法?我可以只传输object<T>,而不是ArrayList一个元素吗?那么,是getParcelable为了什么?

yor*_*rkw 152

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

假设您有一个类Foo正确实现Parcelable,将其放入Activity中的Intent:

Intent intent = new Intent(getBaseContext(), NextActivity.class);
Foo foo = new Foo();
intent.putExtra("foo ", foo);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

要从另一个活动中获取它:

Foo foo = getIntent().getExtras().getParcelable("foo");
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.