0

Just wondering if I could gather some constructive comments. Well, I am developing a tabbed application with activity group for each tab and related child activities. One of the activities handles a file downloader- waits for the download link from the server, once received download begins in the background. On completion, the file (mostly PDF) opens up in a generic viewer. This part works exceptionally well.

Now, coming to app requirement, I want the activity to run in background. When the user is in the download page, it shows a progress dialog with necessary messages. User, having decided to switch to other screens, moves to other screens. In real, the downloader activity should run in background.

A typical download activity class looks like:

 public class DownloadActivity extends Activity {
    public void onCreate(Bundle state) { //code here}
    public void onPause() {//can anything be done here?}
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
           //go back to previous screens to check other features
           TabGroupActivity parentActivity = (TabGroupActivity)getParent();
           //onBackPressed()- current activity is killed and new one is created
           parentActivity.onBackPressed();
           return true;
        }
        return super.onKeyUp(keyCode, event);
    }
 }

I want to modify the above activity to help it work background while user plays arounds with other screen.

Any thoughts?

Hope it is not very confusing! Let me know in case you don't understand any of the above.

Thanks in advance!

Renjith
  • 3,457
  • 5
  • 46
  • 67

2 Answers2

1

Activity can't and shouldn't work in background. For background operations in Android you should use Service.

There is an article about Android processes and services.

Service doesn't have UI, it should do all the job, in the background. Activity can display current application state(s) to the user, that is changed/modified by Service. When Service finish his work, it could send a Notification, for example. More details in this guide.

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
  • try to read a developer guide (I've provided a link in answer) first, just to understand basic concepts of Android components life cycle – HitOdessit Oct 10 '12 at 13:41
  • I understand service is the best choice for now! However, one quick question. Having set up an Activity which initiates a Service with message handler, what happens if the Activity is killed? For now, there is no callback from Service since the binding Activity is no more. – Renjith Oct 10 '12 at 13:55
  • Activity can be killed by system, but your Service will stay alive and continue it job. When Service finish it work, it could send a Notification to the user, for example – HitOdessit Oct 10 '12 at 13:57
  • I've updated my answer to add link to developer guide about Services and Notifications – HitOdessit Oct 10 '12 at 14:00
  • sure! Thanks Hit. Now I got a breakthrough. I start building with Services. – Renjith Oct 10 '12 at 14:10
  • @Hit, `Activity can't and shouldn't work in background` if I press the home button the activity goes to background and still works, the same applies to `moveTaskToBack()` – Pedro Lobito Apr 12 '14 at 23:53
0

You can subclass AsyncTask (http://developer.android.com/reference/android/os/AsyncTask.html) through which you can do some work in background and post it to current activity through Handlers.

kjurkovic
  • 4,044
  • 2
  • 23
  • 28
  • AsyncTask can be killed by the system, if user leave Activity. For long-running background operations Service should be used. – HitOdessit Oct 10 '12 at 13:32