Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind two Android Activities to one Service?

I would like to ask for some example, where two different activities (button in first activity open second activity), are communicating with one service (AIDL, etc.) I've tried many different tutorials, but they are only about how to make one activity -> one service.

like image 720
lauriys Avatar asked Feb 12 '11 19:02

lauriys


2 Answers

This is probably old, but I'll try to answer it anyways...

In Android, seeing as only one Activity can bind to a Service at a time, and only one Activity can be shown at a time, there is no real reason to want to bind 2 Activities at a time.

But, if you'd like, the best solution is to bind the Service in the onResume() method, and unbind it in the onPause() method. This allows you to give two un-related Activities access to the service, while only having one bound at a time.

like image 92
D4N14L Avatar answered Sep 21 '22 03:09

D4N14L


Each Activity is responsible for binding and unbinding from the Service. This is normally done in onResume / onPause, or onStart / onStop, depending on your requirements. One Activity cannot bind another Activity to a Service. That's just the way it is. :)

like image 24
Rich Schuler Avatar answered Sep 20 '22 03:09

Rich Schuler