Swap-IOS-Android
Swap-IOS-Android

Reputation: 4383

how to start service from fragments

I want to start service from fragment from a list view item. I am trying to call service with:

startService(new Intent(getActivity(),myPlayService.class));

But it wont work at all. How do i call my service from fragments? Is there any another way to start service?

Upvotes: 38

Views: 49901

Answers (3)

Abdul Waajid
Abdul Waajid

Reputation: 13

Make sure to add the service tag in manifest.xml

   <service android:name=".ServieName"/>

The service won't start if the service tag is not properly added in the manifest.

For more info refer android documentation

Upvotes: 0

guest
guest

Reputation: 462

To start service from a fragment use

Java

requireActivity().startService(new Intent(getContext(), ServiceName.class));

Kotlin

requireActivity().startService(Intent(context, ServiceName::class.java)

Upvotes: 3

DroidBender
DroidBender

Reputation: 7892

Replace

startService(new Intent(getActivity(),myPlayService.class));

with

getActivity().startService(new Intent(getActivity(),myPlayService.class));

Upvotes: 119

Related Questions