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

How to pass external object between intents using Parcel

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

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

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];
         }
     };
}

登录活动:

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

主要活动:

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

不,您不能像@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