2

Hi and thanks for your help.

I have the following situation.

My Activity is bound to a Service that performs heavy data retrival from internet via an AsyncTask.

Since the Service is heavy I need to make make sure it is stopped when the user navigates away for the Activity, therefore I make sure to stop the service in onDestroy().

When configuration changes onDestroy() is called - and so are onPause() and onStop()- and subsequently the Service is stopped by onDestroy().

The point is that I do not want the Service tho stop on configuration change.

Please any suggestion very much appreciated!

And thanks for your help!

spaaarky21
  • 6,524
  • 7
  • 52
  • 65
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157

3 Answers3

3

You can call isChangingConfigurations() in onDestroy() on API Level 11 and above. You can refer to this post, How to distinguish if activity is changing configurations.

Maybe you should take another approach. I think you should use a IntentService (which runs in background, no more need for AsyncTask), use that service to retrieve & store data, and then notify the Activities that the data is updated, using a Broadcast maybe.

spaaarky21
  • 6,524
  • 7
  • 52
  • 65
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
0

Try with this :

<service android:name=".yourService"
       android:configChanges="keyboardHidden|orientation"></service>

Apply this attribute to your manifest file.

Short Explanation :

By default, when certain key configuration changes happen on Android (a common example is an orientation change), Android fully restarts the running Activity/Service to help it adjust to such changes.

When you define android:configChanges="keyboardHidden|orientation" in your AndroidManifest, you are telling Android: "Please don't do the default reset when the keyboard is pulled out, or the phone is rotated; I want to handle this myself. Yes, I know what I'm doing".

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • thank you, please can you explain me very shortly what is the puropse of this setting in the Manifest ? Thanks!!! – Lisa Anne Jan 20 '13 at 13:11
  • 1
    @MaurizioPietrantuono see my edit. I just added short explanation for this. – Pratik Sharma Jan 20 '13 at 13:16
  • thank you Pratik! Anyway this can not solve the problem, because even if i tell Android not to destroy the Service on configuration change, it is my onDestroy() that does so. – Lisa Anne Jan 20 '13 at 13:22
  • 1
    @MaurizioPietrantuono I got something relevant to this. Please Have a look at this. This might helpful to you. [ http://stackoverflow.com/a/8361080/556975 ] – Pratik Sharma Jan 20 '13 at 13:33
0

I can suggest two things for your issue.

1) use below tag in your activity(Yes Activity not service) declaration in Manifest file, which will basically avoid recreation of Activity on orientation change. hence onDestroy will not be called.

android:configChanges="keyboardHidden|orientation"

2) you can kill/stop your service on below method in your activity, which in terms will only be called when User want to navigate away from your activity. and You should also stop your service whenever you are starting another activity or so.

 @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
}
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • thanks Ankit! Please, correct me if I am wrong: this way basically I will declare to Android that I will manage the orientation of the device, which means that when the screen rotates the screen will not change at all (and the user therefore will see an upside down screen) – Lisa Anne Jan 20 '13 at 13:27
  • 1
    yes correct. If you want system to change the configuration too. You can use that BroadcastReceiver logic to let Activity know when Downloading is done. and can call stop self(or use IntentService). Thanks!! One more suggestion dont do object clearing task on onDestroy, they suggest onPause for the same. – AAnkit Jan 20 '13 at 13:33