0

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.

PrincessLeiha
  • 3,144
  • 4
  • 32
  • 53
  • Have you tried using a public static variable that could store the state of downloads, which is reset in the onDestroy method? – varevarao Nov 09 '12 at 09:36
  • @varevarao, that way, if the 1st activity downloading is not completed, the 2nd activity will start 3rd thread to download. The 3 threads then will be- UI Thread, 1st activity downloading thread, 2nd activity's downloading thread. I have therefore used a `GLOBAL VARIABLE` to maintain the state of download. – PrincessLeiha Nov 09 '12 at 10:15
  • good question @Pallavi :) keep it up – SagarPPanchal May 13 '13 at 12:09

3 Answers3

2

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.

Zsombor Erdődy-Nagy
  • 16,864
  • 16
  • 76
  • 101
  • good point... I have to download a file from server and compare it with the current file that i have. If there are any changes in the 2 files, start downloading other files, else set the status as download complete – PrincessLeiha Nov 09 '12 at 09:54
  • 1
    This doesn't sound too efficient. You download a file every time the user resumes your application just to check that you have the latest version? How about telling this fact by some other means, e.g. getting the version of the current fileset on the server with a cheaper web-service call? Anyways, I wouldn't tie this logic to the event of the user resuming your application. Consider putting this into a service that runs periodically for example (like once day with AlarmManager). – Zsombor Erdődy-Nagy Nov 09 '12 at 10:00
  • The client wants to get the changes as soon as they happen on the server, there is no webservice involved. We have asked him to, but he is reluctant. If I schedule it once or twice a day, the user won't get the latest data every time, as the updating is quite frequent and is not periodic. – PrincessLeiha Nov 09 '12 at 10:04
  • 1
    This sounds terribly inefficient. How about using Google Cloud Messaging to tell the clients that a new version has arrived? – Zsombor Erdődy-Nagy Nov 09 '12 at 10:06
  • He has already implemented the iOS code, and in iOS, they have a method to get when the application is brought to foreground. Now he is not ready to do any changes for server. So any other work around without extra cost will be appreciated – PrincessLeiha Nov 09 '12 at 10:10
  • Why don't you do like that only for Android Version – Seshu Vinay Nov 09 '12 at 10:12
  • 1
    http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo – Seshu Vinay Nov 09 '12 at 10:15
  • That link may help you. it says when application is brought to background and foreground – Seshu Vinay Nov 09 '12 at 10:15
1

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.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • If i start service in onStart or onResume, each time the activity starts or resumes, the service (downloading) will start. This will increase the CPU usage. – PrincessLeiha Nov 09 '12 at 09:59
  • There's no difference in CPU usage. The service is just one. You won't be starting multiple instances of the service. The activity is just binding to existing service. – Budius Nov 09 '12 at 10:41
1

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.

Binoy Babu
  • 16,699
  • 17
  • 91
  • 134
  • won't work... I am able to get the state of download. THIS IS NOT THE ISSUE. The issue is, after the application is sent to background, and brought to foreground, this variable will not be able to tell me, that i need to start downloading or not – PrincessLeiha Nov 09 '12 at 09:53
  • Why not? If you do know the state of the download (was the download completed?) - you know if you have to start or resume the download. Am I missing something here? – Binoy Babu Nov 09 '12 at 09:56
  • @BinoyBabu yes you are missing the whole issue. The issue is not maintaining the downloading state, I am maintaining it currently, as a result, at a time only 1 thread for downloading is going on.. My issue is: when 1st downloading is finished, and user navigates to next activity, the app gets the downloading state which is `downloading complete`, so it starts downloading again. I want to start downloading only when the application is resumed, not when the activity is resumed – PrincessLeiha Nov 09 '12 at 10:02
  • You could do that too with SharedPreferences. Create another preference called resumed. Set it to true each time an onResume is called and vice versa. You know the rest. – Binoy Babu Nov 09 '12 at 10:06
  • That won't work if the application is running in background and is brought to front. That way, the downloading will never start. – PrincessLeiha Nov 09 '12 at 10:08
  • What do you mean by application brought to front : I guess it's either the user clicks on the launcher icon of the app or on the list of recent apps or on the widget/homescreen-shortcut or whatever. In all these cases onResume of an activity is called. See http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle also http://developer.android.com/guide/components/fundamentals.html – Binoy Babu Nov 09 '12 at 10:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19336/discussion-between-binoy-babu-and-pallavi) – Binoy Babu Nov 09 '12 at 10:28