0

I am new to android programming, and I am trying to send an array of custom objects to a different activity. In the first activity I have a function that creates an intent and adds the array of objects to it:

public void openSchedule() {
    Intent nextScreen = new Intent(getApplicationContext(), Schedule.class);
    nextScreen.putExtra("array", tasks);
    startActivity(nextScreen);
}

However, I'm not sure how to get that data at the next screen.

Is there any way to get that array in the code for the next activity even though "tasks" is an array of custom objects?

Lucas B
  • 101
  • 1
  • 2
  • 10
  • see this link http://stackoverflow.com/questions/10107442/android-how-put-parcelable-object-to-intent-and-use-getparcelable-method-of-bu – N J Jul 24 '15 at 14:29

3 Answers3

5

Did you try it with Gson?

public void openSchedule() {
    Intent nextScreen = new Intent(getApplicationContext(), Schedule.class);
    String arrayAsString = new Gson().toJson(tasks);
    nextScreen.putExtra("array", arrayAsString);
    startActivity(nextScreen);
}

In your second activity, parse the extra data back into the List<T>

public void parseBack(){
    String arrayAsString = getIntent().getExtras().getString("array");
    List<YourType> list = Arrays.asList(new Gson().fromJson(arrayAsString, YourType[].class));
}
azurh
  • 410
  • 4
  • 12
1

Have your custom object implement Parcelable, in order to make use of putParcelableArrayListExtra from the Intent class.

2Dee
  • 8,609
  • 7
  • 42
  • 53
0

You didn't search a lot...

Check this for Parcelable interface : How to send an object from one Android Activity to another using Intents?

edit : This too, https://guides.codepath.com/android/Using-Parcelable

Community
  • 1
  • 1
Will Bobo
  • 408
  • 4
  • 11