0

I'm building a timer app. The timer runs on a service and updates the ui. The app starts the service inside onCreate().

getApplication().startService(intentService);
getApplication().bindService(intentService, serviceConnection, Context.BIND_AUTO_CREATE);

It works. Except it doesn't. Specifically on configuration changes.

I know that the calling activity gets destroyed (onDestroy) and then recreated, onCreate is called again and... bindService() is supposed to bind to the same service. I'm calling it with the same exact Intent, same serviceConnection, from the same (application) context.

It doesn't. It binds to a new service object somehow. (I can see it in the debugger, the object returned to the activity is different). That breaks my timer.

It's interesting to me that when I exit the activity and reenter it through the Notification (it's a foreground service, and the notification brings you to the same activity), onCreate() gets called again, startService and bindService too, but that time, it's somehow able to bind properly to the running Service.

What am I missing?

EDIT: Here are the main parts of the Service code:

public class MyService extends IntentService {
    // variables ...
    public MyService() { super("MyService"); }
    protected void onHandleIntent(@Nullable Intent intent) {
         if (!hasStarted) {
              initializeTimer();
              startTimer();
         }
    }

    public IBinder onBind(Intent intent) {
         return new RecordBinder(this);
    }

    public void onDestroy() {
          super.onDestroy();
          if (timer != null)
              timer = null;
    }
    // there are also a bunch of private methods and stuff...
}
ovalb
  • 555
  • 6
  • 15
  • 1
    Do you unbind from the service? – Pawel Sep 28 '18 at 23:12
  • what do you return from services `onStartCommand`, `onUnbind` and `onRebind`? – Pawel Sep 28 '18 at 23:31
  • You really need to show your Service code. – dominicoder Sep 29 '18 at 02:00
  • @Pawel I call `getApplication().unbindService(serviceConnection)` in the onDestroy method of the activity. Nothing changes. – ovalb Sep 29 '18 at 10:57
  • @Pawel I'm extending an IntentService which only has the onBind() method. May that be the issue? I would assume an intent service would be able to allow proper binding / rebinding, since it offers the possibility of overriding onBind.. – ovalb Sep 29 '18 at 11:00
  • `IntentService` are not intended for binding and will have undefined behavior when you do so. Extend `Service` directly and return `START_NOT_STICKY` from `onStartCommand`. – Pawel Sep 29 '18 at 11:11
  • @Pawel Well, first of all, in the official docs it doesn't seem to ever tell you to not use IntentService for binding. Anyways, I tried your way: still same error. After config change the bindService() returns false. – ovalb Sep 29 '18 at 12:01
  • In my last comment I was wrong, bindService actually returns true, but I still don't know why ui isn't updating – ovalb Sep 30 '18 at 08:54

0 Answers0