Ivan Javorovic
Ivan Javorovic

Reputation: 263

Periodically adding elements to ArrayList

I have a ArrayList inside my IntentService to which I add elements such as a users Name and Message.

private ArrayList<ShoutData> shoutList = new ArrayList<>();

A new Name and Message get added each time the service receives a new Notification.

 ShoutData data = new ShoutData();
 data.setName(name);
 data.setShout(shout);
 shoutList.add(data);
 resultIntent.putParcelableArrayListExtra(SHOUT_ARRAY, shoutList);

So when the notification is clicked it should lead to a activity where i can retrieve the whole ArrayList (With multiple Names and Messages). However only the Name and Message from the last notification ends up being inside the ArrayList. What I need is to get all of the Names and Messages from the Users from which I have received the notifications. How can I fix this issue? I think the problem is in the fact that the ArrayList gets recreated each time the new notification comes and only the Name and Message from the current notification get added(The Names and Messages need to be stored until the notification is clicked/the intent is called). So the elements are not getting added all at once but one by one as each notification is received. By the way the pendingIntent:

PendingIntent.FLAG_UPDATE_CURRENT

If any other code is needed please comment.

Upvotes: 0

Views: 92

Answers (1)

vvg
vvg

Reputation: 6385

That is possible. You should initially create your array inside for ex. some home activity. Also you can make some singleton class with global config (your array) and access it in IntentService.

Upvotes: 1

Related Questions