简体   繁体   English

如何将其中包含嵌套类的类的对象传递给活动?

[英]How can I pass an Object of a class that has nested classes within it to an Activity?

Let's say I have the following Class:假设我有以下课程:

public class Person {

private String name;
private String phoneNumber;
.
.
.
private Address address;

}

Where Address is a different class.其中地址是一个不同的类。 I want to pass an object of that class from one Activity to another in android, is that possible?我想将该类的对象从一个 Activity 传递到 android 中的另一个,这可能吗? and if so how?如果是这样怎么办? Thanks in advance.提前致谢。

This can be achieved by changing your class so that it implements either Serializable or Parcelable .这可以通过更改您的类以实现SerializableParcelable来实现。 You can then add the object to a Bundle, and retrieve it from the intent you use to start the new activity.然后,您可以将对象添加到 Bundle,并从用于启动新活动的意图中检索它。

// setup data
Intent intent = new Intent(this, SecondActivity.class);
intent.putSerializable("my_key", personObject);
startActivity(intent);

// retrieve data in second activity
Person myPerson = getIntent().getExtras().getSerializable("my_key");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM