Question

In an Android library I'm writing, I have a queue which has elements constantly being enqueued and dequeued. A required specification is that no elements in the queue are lost. So, if the application closes, I have to save the queue somehow.

I have two options:

1) Ideally, I could save the queue into a SQLite database when the application closes. However, I'm not sure how to detect this, or even if it's possible. In this manner, I can reload the queue elements which were never dequeued back into the queue the next time the app opens. If someone could tell me how to detect the application close from a library (not an Activity), it would be very helpful.

2) If that's not possible, I could write the queue straight into the database for every insertion and removal. However, this is terribly inefficient, and is too slow for my library.

What's the best way to handle this problem?

Was it helpful?

Solution

So, if the application closes, I have to save the queue somehow.

Update the persistent store when entries are added to the queue. For example, Square's Tape offers a persistent queue implementation.

However, I'm not sure how to detect this, or even if it's possible.

It is not possible. The closest thing that Android has to "application closes" is when the process is terminated, and you are not notified about this in advance.

However, this is terribly inefficient, and is too slow for my library.

It works for Square. Their app has been downloaded millions of times and has a ~4.5 star rating on the Play Store, and it uses Tape.

Note that AFAIK Tape does not write to SQLite, though.

OTHER TIPS

You can't detect when an Application is closed, however this work should be done in a Service in which you can override the onDestroy() method.

In onPause event, do the following

if (isFinishing()) // This tells you the app is closing.

Only in this case save it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top