Skip to content Skip to sidebar Skip to footer

Managing Background Download : Android

I'm designing a news app where I need to download fresh articles and their detailed stories whenever user opens my app. I'm doing all of this a background thread. My prime focus wa

Solution 1:

I don't think you have to do that : either the user is pressing the "Home" button (which most people do) and then it's common for apps to keep running in background, and as so to still be easily accessible to the user in the state they left it. Either you provide a "close app" button which really kills the app, which will also kill every kind of thread created by the app and you don't have to worry.

If you really want, you could capture the "Home" clicks, and use those to kill the app before returning to home, which is a nice thing to do if your app has 0 initialization time.

Solution 2:

But I've no idea where to clear it. I tried doing it in onDestroy() of my MainActivity.

In order to know if the activity is destroyed because the user finished it (with Back) or Android will re-create it, you could use isFinishing(); Something like:

protected void onDestroy() {
 super.onDestroy();
 if(isFinishing()) {
 // stop the news feed download
}
}

Or better, stop the feed download in finish():

publicvoidfinish() {
  // stop the news feed downloadsuper.finish();
}

To go back to what you said above with:

I'm very clear about initialization of this flag variable. I've initialized it in onCreate() of Application subclass since it is the point where application starts.

Even if the activity is finished, the application is very probable to still live. The Android OS will decide when to kill it. So you will initialize the download once the app starts, then you will stop it on onDestroy() or on finish() within Activity, depending on your desire, but if the application doesn't stop (most probable) and you're re-entering again in the news activity you should be starting the news download.

I would rather initiate the download in the background in onCreate(Bundle savedInstance), but when savedInstance is null (so I know this is the first create of this activity) and stop it (if hasn't stopped already by itself) in finish();

Hope it helps!

Solution 3:

To begin with for downloading datas from webservice (json or xml) you should use AsyncTask (easy to use)

so what i mean was, to clear your flag with ondestroy(), for when the application is exited, and maybe you can catch when the home button is pressed

Override the below method in your Activity,

@OverridepublicvoidonAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

And now handle the key event like this,

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        //do something
    }
    if(keyCode==KeyEvent.KEYCODE_BACK)
    {
        //do somethingfinish();
    }
    return false;
}

Post a Comment for "Managing Background Download : Android"