1

I'm looking for the best practise, for making background updater. I have this sequence of activities

SplashScreen -> LoginScreen -> MainActivity

What i need is to perform update before MainActivity has started. I'm using volley lib for download. I want Activity/Singleton/Service/Something else, which starts simultaneously with SplashScreen. And start of MainActivity have to wait until the background Updater has finished. I want to

Updater object behavior: Check version, if newer version exists, download archive with sources. Try apply changes (parsing xml and lots of things that should crash). Apply changes. Start app.

My thoughts: 1) Make updater an Activity: But i want to download o lots of bits and 5 seconds of splashscreen wait should slow down this process. As i know, background activity doesn't exist. (Yeah, it does, and its called service...) 2) Make them an Service: I never used service, so i'm not sure it's the best way. After update there is 24 hours off timer. Is even possible to start Activity from service? 3) Singleton (static class, or something, what is not activity, nor service): As service, but there should be problem with acces options that needs context to it's running. If i get it, change context from Splashscreen to loginscreen shloud be the problem. 4) AsyncTaks: As previous, how to wait until asynctaks is done to start activity?

Did i miss something? Is some practise which fits my problem?

Can anyone give me some example of classes and their connection to each other, which would be the best solution of this problem?

l0v3
  • 963
  • 7
  • 26

1 Answers1

2

Option 4, AsyncTask, is a good candidate for what you want to do.

Option 1 (Activity) is not good because activities can be destroyed by the system for multiple reasons (e.g. screen rotation). Plus code invoked from activity lifecycle methods (onCreate, etc) execute on the UI thread. You do not want to perform network activity on this thread. It is a bad practice that leads to sluggish UIs. Newer versions of Android will throw exceptions if you attempt to do so (read up on Safe Mode).

Option 2 (Service) is not a bad one, but coding a service is not as simple as an AsncTask. Plus services tend to be used for longer running operations. Here is an SO post that explains this well: Android: AsyncTask vs Service.

Option 3 (Singleton) is not good as Android can kill your process at any time if memory is low. If your singleton has state, when the process is re-created, that state will be lost. There are ways to work around this, but they are difficult and error-prone.

So I suggest you read up on AsyncTasks. They are really quite useful in Android. They provide you with a thread from a pre-existing thread pool, and class methods that help you to ensure that your code is executing on the right type of thread (e.g. UI or background).

=========

So as this applies to your case:

  1. Launch the AsyncTask at the appropriate time for your app. Most likely in the LoginActivity.
  2. In the "doInBackground" method, make your download call. Loop until the download is complete. When complete, let the method return.
  3. Android will then call your task's onPostExecute method on the UI thread. Here you can end the current activity and start the new one.

Of course this is just one suggestion, and without knowing your app, I cannot say that it is optimal. Hopefully this gives you a taste of AsyncTask usage.

Community
  • 1
  • 1
EJK
  • 12,332
  • 3
  • 38
  • 55
  • About option(4): If i start async task from activity which ends, should i check the proces of updating? Should i detect the end of task in another activity? How to start new activity then? Should i reuse async task for "manual" use? Means manualy update inside MainActivity? – l0v3 Dec 02 '13 at 03:29
  • 1
    I suggest not doing that. Instead call it in one activity, and wait until the AsyncTask completes before ending the activity. It would be possible to do what you are asking, but it greatly complicates the matter. – EJK Dec 02 '13 at 03:32
  • And how about separate to two async task. Check version, run while splashscreen is shown. And update data, runs after login, before MainActivity and should be skipped? – l0v3 Dec 02 '13 at 03:41