Bind service to activity or fragment?

末鹿安然 提交于 2019-12-02 21:35:27

Bind the Service to your activity and not the Fragment. The description of your application, one activity with multiple Fragment that are swapped in and out, makes this the most (and really only) practical approach.

When you bind a Service to an Activity you are tying its lifecycle to that of the Activity. See Bound Services. Each time you add or remove a Fragment in your activity that Fragment is created and destroyed. You do not want to try to link a service to this process because then you would have to create and destroy the service each time a new fragment is created or destroyed.

Instead bind to the host Activity. You can then interact with your host activity from your fragments with an interface to access the bound service or by Intent.

I think the cleaner architecture is to bind directly from the fragment. Regarding the problem outlined in Rarw's answer, you can bind to the service from your activity and from your fragments too. This way you are sure that the service will be there until the activity is not destroyed.

I can see two main advantages in connecting from the fragment:

  1. Service connection is async, so inside the fragment you are never really sure that the service you are getting from the activity is not null. This will lead you at least to some null pointer checks and to some mechanism to refresh the fragment both when it's created and when the service connects (so you are sure you will display the data no matter which of the two happens first).

  2. You do not depend on the particular activity your fragments lives in. To get the service from the activity I think you are doing a cast to the activity specific class. You could create an interface like BoundActivity with a method like getBoundService to obtain the service from it, but I think it's an overhead considering the advantage of point 1. And what if you have multiple services.

UPDATE

Here is a very simple project showing this. https://github.com/ena1106/FragmentBoundServiceExample

You can access your activity from a fragment by getActivity()

you can tray using the event bus pattern with this library , eventbus publish/subscribe pattern.https://github.com/greenrobot/EventBus check the project site for more information.

you can send events from/to service to active or fragments

IF you need to get some data from your service to your fragment at the beginning of the fragment lifecycle, the onServiceConnected on activity could not be called yet, mainly when you rotate your device, and you'll get a NPE. I think is a good idea let fragment make his own connections since the service is started with startService() and not with bindService().

I bind service in My Host Activity,and pass Ibinder's object by Bundle which is setted in arguments.

and my code may like this:

private ServiceConnection mConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        //put service in bundle
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};

The only method I have found to be reliable is to use a loader in Fragment:

  1. create a Loader in the fragment
  2. use the context of the Loader (set to activity in initLoader at when the Fragment's onCreate is called)
  3. bind the service in onStartLoading, using a ServiceConnection which calls forceLoad() as soon as the service is bound
  4. optionally register callbacks in onStartLoading/onStopLoading
  5. unbind the service in onStopLoading
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!