Question

I'm wondering if there is a way to see if the application is resumed from memory.

I need to do download a file upon startup, but I don't want to use the onResume() because then it will be triggered when going back in stack as well.

Only when the app is resumed from e.g. the home screen.

Has anybody done anything similar?

Was it helpful?

Solution

Use a static field (e.g. a reference to the file contents, or a boolean indicating that it was recently downloaded). If the app is killed and restarted, the static field will be null. If it's just progressing through the app lifecycle without dying, the field won't be reset.

This may or may not be quite what you need -- there's no guarantee that an Android app won't run for days, or that it won't be completely killed and restarted frequently. What you might actually want to do is store a timestamp alongside the file, and re-download the file if the timestamp is older than a certain threshold.

OTHER TIPS

Use onCreate. This only gets called when the apk is started (not necessarily from the launcher) because it was destroyed and needs to be recreated.

However this means if the user starts the app from the launcher but is was already stopped it will not download the file.

read the Life Cycle Documentation for more information.

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