Android Studio
[Android Studio] 다른 액티비티로 데이터 전달 시 클래스의 객체를 전달하는 방법
dong_su
2023. 12. 28. 12:06
public class Contact implements Serializable {
public String name;
public String phone;
public String address;
public Contact(String name, String phone, String address) {
this.name = name;
this.phone = phone;
this.address = address;
}
}
먼저, 전달 할 클래스로 가서 Serializable을 구현(implements)한다.
보낼 때
Intent intent = new Intent();
Contact contact = new Contact(name, phone, address);
intent.putExtra("contact", contact);
setResult(300, intent);
보낼 때는 다른 때와 같이 putExtra() 함수에 key와 데이터를 보낸다.
받을 때
Contact contact = (Contact) o.getData().getSerializableExtra("contact");
직렬화 한 객체를 Contact 타입으로 형변환해야 오류가 안난다.