简体   繁体   中英

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? and if so how? Thanks in advance.

This can be achieved by changing your class so that it implements either Serializable or Parcelable . You can then add the object to a Bundle, and retrieve it from the intent you use to start the new activity.

// 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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