PrincessLeiha
PrincessLeiha

Reputation: 3174

How to get the onResume of Application?

My application has 3 activities-Main menu, sub menu, detail. Every time the APPLICATION resumes, I want to get some intimation so that I can start downloading some files.

Right now what i am doing is- on each activity onResume, i check if downloading is already going on or not. If it isn't then i start downloading. This way, whenever i start next/previous activity, if the downloading is not going on, it starts downloading. Meaning, once downloading was completed, user navigates to next page, downloading started again.

I want to prevent this behaviour otherwise there will be unnecessary internet usage.

If i maintain a global (application level) variable which keeps a track of download state, even after i resume the application, the value is not re-set.

Any suggestion as to how to get the onResume of application.

Upvotes: 0

Views: 154

Answers (3)

Binoy Babu
Binoy Babu

Reputation: 17139

You can use SharedPreferences to store data like :

When download is completed :

  SharedPreferences settings = getSharedPreferences(MY_PREF, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putBoolean("download_done", true);

If you want to check if the download is done form another activity:

   SharedPreferences settings = getSharedPreferences(MY_PREF, 0);
   boolean downloadDone = settings.getBoolean("download_done", false);
   if (!downloadDone) {
   // Code to start/resume download.
   }

You can use this from any Activity, Service etc. For more details go here.

Upvotes: 1

Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

Well how about checking for the downloaded content before starting the download process? There're multiple ways to do this, you didn't provide much details on the nature of the content to download.

For example: check for the downloaded file in the FS, set a persistent flag somewhere (using SharedPreferences, SQLite db, whatever) that marks if the content is already downloaded, etc.

Upvotes: 2

Budius
Budius

Reputation: 39846

You can't. The application doesn't resume. The application only Creates and Destroys. The activities have that complex life cycle.

the best approach for me it seems to be a service that each Activity bind to onResume and the service takes care of the downloading.

Upvotes: 1

Related Questions