5

i am trying to pass an arraylist back to my parent activity

Here is the simple code.

private ArrayList<Receipt> receipts = new ArrayList<Receipt>();


 Intent data = new Intent();
 data. // what to do here?
setResult(RESULT_OK, data); 

//************************************

This is basic receipt Class

public class Receipt {


    public String referenceNo;
    public byte[]   image;
    public String comments;
    public Date   createdOn;
    public Date   updatedOn;

Tell me how can i add it in my intent and how can i retrieve it back in parent activity from

onActivityResult(final int requestCode, int resultCode, final Intent data)
amit
  • 175,853
  • 27
  • 231
  • 333
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • 1
    Have you checked this question? -> http://stackoverflow.com/questions/5207510/android-passing-an-arraylist-back-to-parent-activity – Till Helge Feb 23 '12 at 10:01
  • http://stackoverflow.com/questions/4626861/put-object-as-intent-for-next-activity.....similar to this one??? – Shashank Kadne Feb 23 '12 at 10:04

4 Answers4

3

You can use data.putExtra()/bundle.putSerializable() to put custom objects in a Bundle/Intent, however, you have to make your class Receipt implement Serializable (just put implements Serializable after the class name, that's all there is to it with such a simple class. Then to read it back you use getSerializable().

Note: I don't think that the Date-class is serializable, you might want to replace those with a long representing time in milliseconds instead ( you can use Date.getTime() for that).

Example:

public class Receipt implements Serializable{
    public String referenceNo;
    public byte[]   image;
    public String comments;
    public long   createdOn;
    public long   updatedOn;
    //...
}

//Put the list in the intent
List<Receipt> list = ...
Intent data = new Intent();
data.putExtra("tag", list);

//Read the list from the intent:
list = (List<Receipt>) data.getSerializableExtra("tag");
Jave
  • 31,598
  • 14
  • 77
  • 90
0

Your solution is to use parcelable. Go through the following links:

link1, link2, link3

AndroDev
  • 3,236
  • 8
  • 35
  • 49
0

You can simply pass your ArrayList and retrieve back.

For example:

ArrayList<Receipt> list = new ArrayList<Receipt>();
intent intent = new Intent();
intent.putExtra("Hello", list);

But make sure you have to implements Serializable interface in your Receipt class.

i.e.

public class Receipt implements Serializable
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

In case your ArrayList is going to be big passing it as Intent data may cause problems such as application not responding. This is because it'll take time for the OS to pass all the data.

Instead, you can create your own Application class (you'll have to register it in your Manifest as well). Then in this class you can have

class MyApplication extends Application {
...
public static Object mData;
}

And you can then do MyApplication.mData = receipts and ArrayList<Receipt> receipts = (ArrayList<Receipt>)(MyApplication.mData) as appropriate.

Since your MyApplication object will exist through the whole app lifecycle this method is safe enough and it's recommended in books on Android (among other ones).

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • 1
    this method is not recommended – Selvin Feb 23 '12 at 10:15
  • it's one of the proposed methods of data exchange in Pro Android 3 book and elsewhere. Anyway, what's bad about this method that you're saying its not recommended. Try to pass a a very large array via intent data and you will see ANR error. – Alexander Kulyakhtin Feb 23 '12 at 13:03
  • first: i took a look at this book and well i can't say good things about it. second: global object will be not freed until application's end. thats why thrid: this problem is more complicated ... why for FSM sake using intent to passing large array at all. if there is a need for this it means that application was bad designed. in my opinion in this case we should use ContentProvider and pass only Uri (Intent.getData()), after that ContentProvider "asked" with this Uri should return cursor with all data that we need – Selvin Feb 23 '12 at 13:35
  • One can explicitly say mData = null or one can use a WeakReference. ContentProvider seems to be unrelated. I liked the book, although, surely, there may exist a variety of opinions. – Alexander Kulyakhtin Feb 23 '12 at 14:29