2017-04-20 14 views
1

Consts이라는 사용자 정의 객체가 있습니다. 여기에는 사용자 및 외부 라이브러리 객체에 대한 정보가 포함됩니다. 나는 사용자 인증 후 LoginActivityConsts 개체를 만들고이 개체를 내 MainActivity에 전달하려고합니다.Parcel을 사용하여 인 텐트간에 외부 객체를 전달하는 방법

인 텐트간에 외부 개체가 포함 된 사용자 지정 개체를 전달하려면 어떻게해야합니까?

Consts 객체의 일부 :

public class Consts implements Parcelable { 

    //Created by me. I can pass this object to MainActivity. 
    UserInformationClass UserInfo = new UserInformationClass(); 

    //This is external object. 
    SweetAlertDialog AlertDialog; 
    String AppVersion = "v2.0"; 
    String OSVersion = ""; 


    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeParcelable(this.UserInfo, flags); 
     dest.writeString(this.AppVersion); 
     dest.writeString(this.OSVersion); 
    } 

    public Consts(SweetAlertDialog alert) { 
      this.AlertDialog = alert; 
    } 

    protected Consts(Parcel in) { 
     this.UserInfo = in.readParcelable(UserInformationClass.class.getClassLoader()); 
     this.AppVersion = in.readString(); 
     this.OSVersion = in.readString(); 
    } 

    public static final Parcelable.Creator<Consts> CREATOR = new Parcelable.Creator<Consts>() { 
     @Override 
     public Consts createFromParcel(Parcel source) { 
      return new Consts(source); 
     } 

     @Override 
     public Consts[] newArray(int size) { 
      return new Consts[size]; 
     } 
    }; 
} 

LoginActivity :

public class MainActivity { 

Consts consts; 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Bundle data = getIntent().getExtras(); 
    consts = (Consts) data.getParcelable("ConstsObject"); 
    //I can get consts object but consts.AlertDialog is null :(
} 
+0

확인 내 대답 : http://stackoverflow.com/a/43424961/6756514 –

+1

@DivyeshPatel,'Item' 객체는'String' 값만 포함합니다. _External_ 오브젝트 전달을 원합니다. – NoName

+0

'Parcel'을 위해 무엇이 필요합니까? 당신의'Consts' 클래스는'Parcelable'이므로'Intent' 엑스트라를 통해 전달합니다. – pskink

답변

1

아니, 당신은 @ user1643723 같은 외부 개체를 전달할 수 없습니다 말했 : MainActivity가

public class LoginActivity { 


    Consts consts = new Consts(new SweetAlertDialog()); 
         . 
         . 

    private void LoginCompleted(JSONObject json) { 
      consts.getUserInfo().setName("Cristiano"); 
      consts.getUserInfo().setSurname("Ronaldo"); 
      consts.getUserInfo().setTokenID("7"); 
      consts.getUserInfo().setTokenTimeOut("100"); 

      Intent i = new Intent(getApplicationContext(), MainActivity.class); 
      i.putExtra("ConstsObject", consts); 
      startActivity(i); 
      finish(); 
    } 
} 

.

당신은 Singleton 사용할 수 있습니다

//-- this must be your Consts file 
public class Singleton { 
    private static final Singleton instance = new Singleton(); 
    SweetAlertDialog AlertDialog; 
    // Private constructor prevents instantiation from other classes 
    private Singleton() { 

    } 

    public static Singleton getInstance() { 
     return instance; 
    } 

    public SweetAlertDialog getAlertDialog() { 
     if (AlertDialog == null) 
      AlertDialog = new SweetAlertDialog(); 
     return AlertDialog; 
    } 

    public void setAlertDialog(SweetAlertDialog alertDialog) { 
     AlertDialog = alertDialog; 
    } 

} 

당신은 어디서나 액세스 할 수 있습니다

Consts consts = Consts.getInstance(); 

Wikipedia:Singleton pattern

+1

답변 해 주셔서 감사합니다. – NoName