如何使用Parcel在意图之间传递外部对象

时间:2017-04-20 07:16:41

标签: android android-intent parcelable parcel

我有一个名为Consts的自定义对象。它包括一些关于用户和外部库对象的信息。我在用户身份验证后在Consts创建LoginActivity对象,我想将此对象传递给MainActivity

如何在意图之间传递包含外部对象的自定义对象?

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 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();
     } 
}

MainActivity:

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 :(
}

1 个答案:

答案 0 :(得分:2)

不,你不能像@ user1643723告诉你的那样传递外部对象。

您可以使用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