0

I have app with multiple fragment.I running asynctask from fragment A and want stop it from fragment B How can I resolve this problem ? enter image description here

enter image description here

Nam Nguyen
  • 317
  • 4
  • 18

1 Answers1

2

I don't think you can use either use AsyncTask or AsyncTaskLoader for that. The easiest solution would probably be to do your download work in an IntentService, and communicate with it from the different activities.

urandom
  • 1,147
  • 2
  • 12
  • 21
  • My usage a a bit different, but here's my service: https://github.com/urandom/gearshift/blob/master/gearshift/src/main/java/org/sugr/gearshift/service/DataService.java#L84 and here's how i tell it what to do: https://github.com/urandom/gearshift/blob/master/gearshift/src/main/java/org/sugr/gearshift/service/DataServiceManager.java#L164 The broadcast receiver at the end of that file receives messages from the service In your case, the messages could be a list of progresses – urandom Apr 21 '16 at 09:08
  • IntentService is a better approach – Amit Ranjan Apr 21 '16 at 09:10
  • But how IntentService send data back to activity to update listview item from activity A & activity B – Nam Nguyen Apr 21 '16 at 09:15
  • The services sends intents to anyone who wants to listen via: LocalBroadcastManager.getInstance(this).sendBroadcast(INTENT); any activity can then receive these events using a broadcast receiver: LocalBroadcastManager.getInstance(context).registerReceiver(RECEIVER); when you receive such an intent in activity A or B, you update your UI accordingly – urandom Apr 21 '16 at 10:05
  • I have try your solution but it still not resolve my problem – Nam Nguyen Apr 21 '16 at 15:20