2

I am starting android service by startService() in Activity A. I have multiple activities in my application. Here the problem is with onDestroy() of Activities.

What I am doing is generating an event in onDestroy() of activity. Application class is a subscriber of this event and I stopping this service in Application class. I know onDestroy() is not guaranteed to be called every time when the application is closing.

I also need to keep service running till application is running. And what is the best way to stopping service on the closing of application?

I am using otto library for event bus.

Vijay Vankhede
  • 3,018
  • 1
  • 26
  • 46
  • 1
    Try startService in onResume() and stopService() in onPause() instead? – Jiyeh Jan 13 '16 at 07:43
  • 1
    But it will stop service onPause of one activity. – Vijay Vankhede Jan 13 '16 at 07:44
  • 1
    http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background/5862048#5862048 , this will provide direction , using application class – geniushkg Jan 13 '16 at 07:46
  • Okay then you may try put it in onStop(). See Activity lifecycle in http://developer.android.com/intl/zh-cn/reference/android/app/Activity.html – Jiyeh Jan 13 '16 at 07:50
  • do not start the `Service` then (startService) but rather bind to it (bindService) – pskink Jan 13 '16 at 07:58
  • @pskink if I use bindService, unbindService in onResume() and onPause(). then service will be stopped when on client is bound to it. – Vijay Vankhede Jan 13 '16 at 08:16
  • read this: http://developer.android.com/intl/es/guide/components/bound-services.html#Additional_Notes – pskink Jan 13 '16 at 08:17
  • Thank you @pskink. You are at the right point. Here I tried same. I tried to bind an activity to service in onCreate() and unbind it in onDestroy(). But problem is that in Android it's not guaranteed that onDestroy() will be called on the closing of application. So there are situations when onDestory() isn't called and Service keeps running. – Vijay Vankhede Jan 13 '16 at 08:30
  • no, there is no such case, first of all find the following in the link i posted *'''When your client is destroyed, it will unbind from the service, but you should always unbind when you're done interacting with the service or when your activity pauses so that the service can shutdown while its not being used.'''*, secondly `Activity#onDestroy` is not called only when the whole process is killed, so the `Service` is killed too – pskink Jan 13 '16 at 08:40

3 Answers3

0

You can start Service in HeadlessFragment and add this HeadlessFragment in you activity

Example for implementing and understanding headless fragment http://luboganev.github.io/blog/headless-fragments/

Hope this helps.

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
0

Implement ActivityLifecycleCallbacks in your Application class:

public class MyApplication extends Application implements ActivityLifecycleCallbacks {

    @Override
    public void onCreate() {
        super.onCreate();

        // initialize anything application wide

        // this is important, otherwise it won't work
        registerActivityLifecycleCallbacks(this);
    }

    @Override
    public void onActivityResumed(Activity activity) {
        // your application is running, do something
    }

    @Override
    public void onActivityPaused(Activity activity) {
        // your application was closed, stop any service or something else.
    }
}

This is the best way to keep track of the lifecycle of the whole application.

Loolooii
  • 8,588
  • 14
  • 66
  • 90
0

You can use ActivityLifecycleCallbacks or TRIM_MEMORY_UI_HIDDEN depending on your requirements to detect Activtys' visibility.

csenga
  • 3,819
  • 1
  • 21
  • 31