AggieDev
AggieDev

Reputation: 5045

Storing files with an AlarmManager and BroadcastReceiver

I have an app that does a task every minute and then adds that info to an ArrayList stored in the MainActivity of the app. While the app is running, even in the background, this works perfectly. However when I destroy the app and let the AlarmManager repeat every minute in the background, for some reason the list is wiped each time new info is added, meaning that the next time I open the app there is always only one object in the ArrayList, which was the last one created by the last AlarmManager that went off.

This event every minute is handled in a BroadcastReceiver which calls a method in the main activity to add to the arraylist stored there. Could this possibly be why my data is wiped whenever the app isn't running? Each time I want to add new data to the list, I have the app fetch the current list from storage, which fetches the list correctly until the app isn't running, then it fetches an empty list each time.

Upvotes: 0

Views: 50

Answers (2)

Solution
Solution

Reputation: 602

Alarm Manager will not dismiss previously set alarm when your app close. So it will invoke as per last set time. So, Please make sure when you close the app, cancel previously set alarm and you can start that alarm on app launch or as per your need.

Upvotes: 0

dipali
dipali

Reputation: 11188

when your app is in background then your arraylist will be empty.you should store your arralist into sharedpreferences.so whenever you have to retrieve your data then you can fetch data from shareprefernces.

please check this below link:

reference link

i hope its useful to you.

Upvotes: 2

Related Questions