-1

I have a problem passing an arraylist of complex object between 2 activity, the object (ObjA) is something like this:

ObjA:
-String
-Array of ObjB

where ObjB is:
ObjB:
-String
-Array of ObjC

where ObjC is:
ObjC:
-String
-String

the 3 object are serializable:

public class Obj implements Serializable{
private static final long serialVersionUID = 1L;
}

I try to pass the object as a normal extras but the app crash without any log, how can I pass this array?

The 3 obj are serialazible, they have this form: public class Materia implements Serializable{

private static final long serialVersionUID = 1L;

String titolo;
String icona;
ArrayList<Sezione> sezioni;

public Materia (){}

public String getTitolo() {
    return titolo;
}

public void setTitolo(String titolo) {
    this.titolo = titolo;
}

public String getIcona() {
    return icona;
}

public void setIcona(String icona) {
    this.icona = icona;
}

public ArrayList<Sezione> getSezioni() {
    return sezioni;
}

public void setSezioni(ArrayList<Sezione> sezioni) {
    this.sezioni = sezioni;
}

}

francesco.venica
  • 1,555
  • 2
  • 17
  • 47
  • 1
    Please show your code of passing the object – Mohammad Ashfaq Feb 21 '14 at 10:07
  • are also ObjB and ObjC serializable? – Blackbelt Feb 21 '14 at 10:09
  • Consider using a Parcelable (http://developer.android.com/reference/android/os/Parcelable.html). There are quite a few tutorials on how to create one. I believe you can also create a parcelable of parcelable's i.e. Obj(A|B|C) each being a parcelable and contained within the other. – Vino Feb 21 '14 at 10:25

2 Answers2

2

1. Using Application class

You can do this using your Application object. This way, you can define a getter and setter method in your application class and use them in activities:

public class MyApp extends Application {
    private Object obj;
    public void setObject(Object obj) {
        this.obj = obj;
    }
    public Object getObject() {
        return obj;
    }
}

Usage in activities:

MyApp app = (MyApp) getApplication();
app.setObject(/* your complex object */);

And in your second Activity:

MyApp app = (MyApp) getApplication();
Object complexObj = app.getObject();

This is a bad approach. User when switches to another app, Android may kill your app (i.e. the process your app in running in), specially when device is running on low memory. After your app being killed, if user comes back to your app, the Application class is re-instantiated and thereby the obj reference inside being null.

2. Make your complex object implements Serializable or Parcelable

Refer to: Android: Difference between Parcelable and Serializable?

Community
  • 1
  • 1
frogatto
  • 26,401
  • 10
  • 73
  • 111
  • 1
    with this method all work, but there is a way to evoid the object distruction? – francesco.venica Feb 21 '14 at 11:07
  • @kikko088 This object destruction is not an issue in your case. Because in your case you immediately start second activity and then in second activity you retrieve it, So no loss occurred! The loss only may occur when duration between save object and retrieve it be a lot – frogatto Feb 21 '14 at 14:37
  • ok, but you say that "The disadvantage of this approach is when your application dies (because of Android mechanisms such as low memory etc), your saved object also dies.", thinking for a future performance improvement I want create this object only the first time that I open the application (is a long JSON that take some second with old devices), but in this case I need to recreate every time android destroy it. ps. I know that is better open another thread, but I try to solve the problem here! thank you for your time! – francesco.venica Feb 21 '14 at 15:22
  • @kikko088 Android only kills your application in critical situations, But if you only want to use this method to pass complex object between activities, there is no risk at all, However if you found my answer helpful please accept it – frogatto Feb 21 '14 at 15:47
1

If all the three objects are serializable then,

In Calling activity:

ObjA a = new ObjA();
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("t", a);
startActivity(i);

In called activity:

ObjA a=(ObjA) getIntent().getSerializableExtra("t");
frogatto
  • 26,401
  • 10
  • 73
  • 111
naidu
  • 350
  • 2
  • 8