0

I have activityA where I show list of options. Based on the option chosen(Say gender : male), a request is passed on to a Service.

case R.id.male:
                        // do operations specific to this selection
                        Intent explicitGetNameServiceIntent = new Intent(getActivity(), GetNameService.class);
                        getActivity().startService(explicitGetNameServiceIntent);

The purpose of service is to get data from web services and return the list of items(say 10 male names) to fragmentB of activityB (where a new list is populated with received 10 names from web services).

Since the data from web services is got within the onHandleIntent() method (which is a void method) of GetNameService, how can I send the received data from web services to fragmentB

sofs1
  • 3,834
  • 11
  • 51
  • 89
  • You can use broadcast receiver to send the data from service to fragment, you can refer [this](http://stackoverflow.com/questions/22541477/broadcast-receiver-in-fragment-not-receive-intent-from-service) as example – Ichigo Kurosaki Aug 30 '16 at 10:14
  • `BroadcastReceiver`, AIDL, `Handlers` & `Messengers`, `EventBus` or `RxJava/Reactive Programming`... – Mark Aug 30 '16 at 10:16

1 Answers1

2

This question has been answered several times on this platform, there are multiple ways to send data from service to activity & fragment:

  • using Broadcast Receivers as answered here

  • bind the service to the activity as answered here

Community
  • 1
  • 1
Muhammad Farhan Habib
  • 1,859
  • 20
  • 23
  • Thanks. If I can define a list as 'public static' in Activity A, and then send request from Activity A to Service to 'select all members who are male from DB' and update the list in service and then move to Activity B. Will it be possible to access the List in Activity B, as it has been defined as public global variable in Activity A and it has been updated in Service? – sofs1 Sep 01 '16 at 10:51
  • yes you can access but its a loose binding, scope will be static limited. – Muhammad Farhan Habib Sep 16 '16 at 05:05