Skip to content Skip to sidebar Skip to footer

Service Automatic Called On Destroying Activity

I am stuck with the problem of Activity + Service in that I have following number of Activities and Services. Activities: LoginActivity => OrderListActivity => AddOrderActiv

Solution 1:

You need to either run your service in the foreground so when the activity is destroyed so will the service or use a bound service and manage the binding with the activity lifecycle, so it is not continually restarted when the activity is destroyed.

From this android docs tutorial Bound Services

You need to do this for each service.

publicclassCheckAutoSyncReceivingOrderextendsService {
    // Binder given to clientsprivatefinalIBindermBinder=newLocalBinder();

    publicclassLocalBinderextendsBinder {
        CheckAutoSyncReceivingOrder getService() {
        return CheckAutoSyncReceivingOrder.this;
    }
}

    @Overridepublic IBinder onBind(Intent intent) {
        return mBinder;
    }   

From your activity that creates and calls the service, that when it is destroyed you want your service destroyed.

publicclassBindingActivityextendsActivity {
    CheckAutoSyncReceivingOr mService;
    booleanmBound=false;


    @OverrideprotectedvoidonStart() {
        super.onStart();
        // Bind to CheckAutoSyncReceivingOrIntentintent=newIntent(this, CheckAutoSyncReceivingOr.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @OverrideprotectedvoidonStop() {
        super.onStop();
        // Unbind from the serviceif (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    /** Defines callbacks for service binding, passed to bindService() */privateServiceConnectionmConnection=newServiceConnection() {

        @OverridepublicvoidonServiceConnected(ComponentName className,
            IBinder service) {
            // We've bound to CheckAutoSyncReceivingOr, cast the IBinder and get CheckAutoSyncReceivingOr instanceLocalBinderbinder= (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @OverridepublicvoidonServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}   

And manage the service lifecycle. Restart the same service with your timer, do not create a new service.

publicclassExampleServiceextendsService {
    int mStartMode;       // indicates how to behave if the service is killed
    IBinder mBinder;      // interface for clients that bindboolean mAllowRebind; // indicates whether onRebind should be used@OverridepublicvoidonCreate() {
        // The service is being created
    }
    @Overridepublic int onStartCommand(Intent intent, int flags, int startId) {
        // The service is starting, due to a call to startService()return mStartMode;
    }
    @Overridepublic IBinder onBind(Intent intent) {
        // A client is binding to the service with bindService()return mBinder;
    }
    @OverridepublicbooleanonUnbind(Intent intent) {
        // All clients have unbound with unbindService()return mAllowRebind;
    }
    @OverridepublicvoidonRebind(Intent intent) {
        // A client is binding to the service with bindService(),// after onUnbind() has already been called
    }
    @OverridepublicvoidonDestroy() {
        // The service is no longer used and is being destroyed
    }
}

Note START_NOT_STICKY will only prevent the service from restarting if the device is low on memory.

Be mindful that you where you are starting services, just start it once and allow the service to maintain it's own lifecycle until you destroy it with your activity.

This is in reply to your original unedited question, when the app was mysteriously crashing:

You need to destroy the dialog before the context window the dialog is attached to. That will cause a problem. So this is where program flow and the order of closing and cleaning up resources is important. They, frequently have to be destroyed in the reverse order they were created if they are dependent upon parent windows (which is often in the form of a particular activity).

It's difficult to trace your code, so this is a generic answer.

Make use of onPause and onDestroy in your activities.

In all your activities, manage any resources you have created within that activity and with a null check, close them down. Like you have in your service class. If you want to override the parent onDestroy, place your custom code before super.onDestroy.

protected void onDestroy() {

    if(timer != null)
        timer.cancel();

    Log.d(TAG, "Stopping Sending...");

    super.onDestroy();
}

Solution 2:

(1)For Your Dialog:

The solution is to call dismiss() on the Dialog you created before exiting the Activity, e.g. in onDestroy(). All Windows & Dialog should be closed before leaving an Activity.

(2)For Your service autostart:

you have to look at the value the service returns from its onStartCommand method. The default value is START_STICKY which will restart the service after it is destroyed. Take a look at the onStartCommand documentation for more details:

Solution 3:

If the process that runs your service gets killed, the Android system will restart it automatically it is default behavior.

This behavior is defined by the return value of onStartCommand() in your Service implementation. The constant START_NOT_STICKY tells Android not to restart the service if it s running while the process is "killed".

You need to Override method onStartCommand() in your service class and move all your code from onStart() method to onStartCommand() method.

According to the Android Documentation:

For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them

onStart() method calls each time when service is restarted but onStartCommand() method will not called if you return START_NON_STICKY.

Don't use onStart() anymore, it's deprecated.

I hope it helps you.

Solution 4:

Services got killed when application got killed (add logs in service onStartCommand() and onDestroy() function and try clearing app from recent list and you will see onDestroy() is called. Android will re-start service if you have returned START_STICKY intent in onStartCommand()).

There are two approaches to fix your problem.

  1. Either make your two services as foreground service.

  2. Instead of using CheckAutoSyncReceivingOrder and CheckAutoSyncSendingOrder to schedule start of another services, you should use AlarmManager to schedule your task.

Post a Comment for "Service Automatic Called On Destroying Activity"