0

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 was that the background thread should stop as soon as user exits the app so as to prevent user for incurring extra download charges.

To achieve this, I initiate background download in my splash screen and I keep on checking for flag variable that lets the background process know if the app is still running.

Now my question is: 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. But I've no idea where to clear it. I tried doing it in onDestroy() of my MainActivity. However, I found that onDestroy() is often called on transition between one activity to another if system needs to free memory. So, doing so there would stop my background thread even when I'm switching across screens and not actually closing the app. How should I deal with this scenario ? Is there a smarter way of handling this ?

Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44

3 Answers3

0

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.

C4stor
  • 8,355
  • 6
  • 29
  • 47
0
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():

public void finish() {
  // stop the news feed download
  super.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!

gunar
  • 14,660
  • 7
  • 56
  • 87
-1

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,

@Override
public void onAttachedToWindow() {
    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 something
        finish();
    }
    return false;
}
An-droid
  • 6,433
  • 9
  • 48
  • 93