Search code examples
androidandroid-serviceandroid-tabhost

onServiceConnected callback of the binded service is not called in TabHost


I use a local Service that is bound to my all activities and when I start an Activity I get a callback:

@Override
        public void onServiceConnected(ComponentName name, IBinder service) {
}

but if I use a TabHost and if activity is being started the next way :

TabSpec spec = tabHost.newTabSpec("Spec");
intent = new Intent(this, TestActivity.class);
spec.setContent(intent);

onServiceConnected callback is neved called.

Why? How to resolve this case?

SUMMARY:

intent = new Intent(this, MyActivity.class);
startActivity(intent); // In this case callback is called


TabSpec spec = tabHost.newTabSpec("Spec");
intent = new Intent(this, MyActivity.class);
spec.setContent(intent); // In this case callback is not called

Solution

  • In a tab instead of bindService(.....) you have to use getApplicationContext.bindService(.....) in order for the onServiceConnected callback to be hit.