Search code examples
androidstartuponresume

Check if application resumed from memory


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?


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.