There are several points to weigh.
In fact @Piovezan not bad, depends on what needs to be done.
If you need to perform functions independent of Activitys create the service without bind, so even with the application closed the service will continue running.
Now if you really need to switch information from Activity to the service it needs to be bind and not add a fragment to it just relay the information.
- The bind is not mandatory but ensures that at the end of Activity the service also stops, in case of dependency avoid errors.
The problem of using Broadcasts is the runtime that should be up to 10 seconds to not lock the application, so it is not feasible to do this unless from the receipt you create an Asynctask or make sure that the execution will not go beyond that!
Could you tell us what functions the service will perform to help us better understand the problem? Another thing, keeping an Activity coupled to a service has poor performance, I suggest rethinking this architecture, maybe using Broadcasts.
– Piovezan
@Piovezan, really it is not good to do
bind
ofService
inActivity
? Do you have any sources to back up?– Wakim
@Wakim Just my own experience. It caused slowness in devices low and mid-end. Perhaps it was characteristic of the application, which performed coupling/decoupling of activities to a service throughout the life cycle of the same. In any case, I switched to Broadcasts and improved a lot.
– Piovezan
Well I use the service as follows. So when creating the service I start a new thread that gets every time makes an online check. This same service is also in charge of updating a database. Once the service finds new data it inserts into the database and gives a broadcast warning the application if it is active to update the new data. Fragment uses the service connection in case bind to query the database ( basically to read the messages )
– Bruno Novais
Isn’t it worth taking a look at Loaders (instead of making bind)? You would have a Loader + Contentprovider (connected to the bank), when the Service updates the Bank using Contentprovider, it can notify the Loader and updates the Fragment automatically (through callbacks).
– Wakim