I got one single Main-Activity in which I create dynamically Pages inside a ViewPager (via FragmentPagerAdapter). So far so good.
Now I got a Service-Implementation I want that activity/those fragments bind to. And this is the point I ask myself which is the best practice.
Both implementations seem to work. But second one offers straighter communication with service.
When testing the second fragments are bound to service on creation. But when I remove all but one they doesn't seem to be destroyed. When I start to create fragments from that point (only 1 fragment after deleting others) again, binding to service only takes place when I create more fragments than I created before.
Perhaps again for better overview.
Is this the behaviour one can expect? Is this the implementation I should choose; obviously straighter communication as on callback-communication?
Thanks for your input in advance!
I prefer the first method especially if you use a Local Service because you have a handle to your service.
Assume that your activity already has a reference to your Local Service called "MyLocalService" (Checkout the Local Service Example). Then from within your fragment you can easily call something like: getActivity().getMyLocalService(). The good thing about this is that your fragment doesn't have to deal with the lifecycle of the service. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With