0

I have custom object named Consts. it's including some informations about user and external library object. I am creating Consts object in LoginActivity after user authentication and I want to pass this object to my MainActivity.

How can I pass custom object containing an external object between intents?

A part of Consts object:

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 :(
}
  • check my answer: http://stackoverflow.com/a/43424961/6756514 – Divyesh Patel Apr 20 '17 at 07:18
  • 1
    @DivyeshPatel, your `Item` object contains only `String` value. I want pass _External_ object. –  Apr 20 '17 at 07:24
  • what do you need `Parcel` for? your `Consts` class is `Parcelable` so simply pass it via `Intent` extras – pskink Apr 20 '17 at 07:39
  • @pskink, `Consts` class contains a `SweetAlertDialog` object. I couldn't pass this object. I want to learn how can i pass external object like `SweetAlertDialog` object. –  Apr 20 '17 at 07:46
  • 1
    You can not put objects that are not parcelable into Parcel. The only parcelable objects are: Binder objects (including IInterface subclasses), file descriptors (after being wrapped in ParcelFileDescriptor), byte arrays and anything that consists from parcelable objects or can be serialized into series of bytes. What is it about AlertDialog, that you want to pass? The text within it? The layout? The theme? Something else? – user1643723 Apr 20 '17 at 08:18
  • @user1643723 AlertDialog is an example. I have a lot of external objects in `Consts` file. so How i can i access same object from all activities. Objective-c has prefix header files and it can accessible from everywhere. I need something like that. –  Apr 20 '17 at 08:51
  • 2
    This is called "static fields" in Java. It is really not Android-specific thing. You can not pass such objects in Intents, but you can pass "orders" (enum or integer extra, Intent action string or Uri), that suggest receiving Activity, which static object to use and what to do with it. Note, that static objects can create memory leaks—they are not garbage collected until you assign `null` to them. So make sure, that you dispose of them, when you no longer need them. Ideally, you don't want to share such objects between Activities at all. It might be a good time to rethink your app design. – user1643723 Apr 20 '17 at 09:30
  • @user1643723, i got it. i don't want to re-create some objects as AlertView, AsyncHttpClient, Custom objects etc. What you recommend to me? –  Apr 20 '17 at 10:39
  • 1
    In most cases you should keep heavy objects as singletons. It is the same as ContentProviders—you don't have to close them, because they will die together with you, when system kills your process. View objects are tricky: they can not be passed between Activities without a lot of ado; theoretically, you can detach them, pass around, then reattach, but this is often not worth a bother (you have to keep both codepaths to both create and reuse them, so may as well just create them each time). The best strategy is to split all reusable state apart and keep it in Parcelable/singlton/database. – user1643723 Apr 20 '17 at 11:09
  • @user1643723 Thank you very much. I am thankful to you –  Apr 20 '17 at 11:11

1 Answers1

0

No, you can't pass external object as @user1643723 told you.

You can use 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;
    }

}

You can access everywhere :

Consts consts = Consts.getInstance();

Wikipedia:Singleton pattern

delavega66
  • 855
  • 2
  • 23
  • 39