인텐트를 사용하여 데이터를 전달할 때 많은 데이터들을 하나씩 보내기는 번거로우니
직렬화를 시켜 한번에 데이터를 전달한다.
class Person{
string name;
int age;
}
Person P = new Person(홍길동, 20);
메모리에 따로 저장되어 있기 때문에 이것을 직렬로 만들어 줘야 된다
그래서 Serializable 인터페이스를 상속 받아 내부맴버변수들을 모두 직렬화하여 객체를 만든다.
class Person implement Serialzable{
private static final long serialVersionUID = 1000000L;
String name;
int age;
}
예제
MainActivity.java
Person p = new Person();
p.setName("홍길동");
p.setAge(20);
Intent in = new Intent(getApplicationContext(),SecondActivity.class);
in.putExtra("person",p);
startActivity(in);
SecondActivity.java
Intent intent = getIntent();
Person p = (Person) intent.getSerializableExtra("person");
String name = p.getName();
int age = p.getAge();
Person.java
public class Person implements Serializable {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
'기초 > android' 카테고리의 다른 글
안드로이드 생명주기 (0) | 2015.05.27 |
---|---|
intent (0) | 2015.05.27 |
어플리케이션 4가지 (0) | 2015.05.26 |
부분 화면 뿌리기 (0) | 2015.05.26 |
RelativeLayout (0) | 2015.05.21 |