2017-03-08 13 views
0

은 내가 Parcelable 인터페이스를 구현 Intent을 보낼 이들과 같은 두 가지 목적이 있습니다양방향 일대일 관계에있는 두 객체를 어떻게 구획합니까?

class Foo implements Parcelable 
{ 
    private Bar bar; 

    public void writeToParcel(Parcel dest, int flags) 
    { 
     dest.writeParcelable(bar, flags); 
    } 
} 

class Bar implements Parcelable 
{ 
    private Foo foo; 

    public void writeToParcel(Parcel dest, int flags) 
    { 
     dest.writeParcelable(foo, flags); 
    } 
} 

어떻게 올바르게 Parcelable 인터페이스를 구현 할 수 있습니까? Foo 클래스의 writeToParcel에서

Foo 필드없이 Bar 필드없이 Foo 인스턴스의 모든 필드와 Bar 인스턴스의 모든 필드를 쓰기 :

답변

1

나는 이런 식으로 할 것이다.

그런 다음 Foo(Parcel in)Bar 인스턴스를 만들고 함께 두 개체를 연결하는 BarFoo 사용 필드의 모든 필드를 읽어보십시오.

+0

감사합니다. 이해합니다. 'DateTime' 필드를 포함하는 다른 객체가 있다면이 필드를 작성하기 위해'writeValue' 또는'writeTypedObject'를 사용해야합니까? – Clyky

+1

'DateTime'이'Serializable'이거나'Parcelable'이고'DateTime'가'Parcelable' 인 경우에만'writeTypedObject'를 쓰면'writeValue'를 사용할 수 있습니다. – RadekJ