0

In my Android application at some point I am starting some animation and make http request for data. Both of those actions are done different thread and after those two actions finish my application needs to perform some other action. My question is are there any good design patterns or solutions to do that?

Currently I have one idea of doing it - create controller which has two boolean flags and makes check on those two before performing action, but maybe there is some better way to solve this problem?

  • Do you need to wait on the animation to finish also. Seems like just checking for api request to finish should be good enough and when the response comes back you can simply dismiss animation. – Naveed Jun 16 '15 at 17:48
  • I'm not sure how you're starting your threads (those details aren't in your question). Java has `Thread.join()`. See http://stackoverflow.com/questions/1361029/waiting-on-multiple-threads-to-complete-in-java – Fuhrmanator Jun 16 '15 at 17:48
  • If its android you should be using [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) which will avoid race condition as `onPostExecute` will always be called on ui thread and you can simply check against an int that you increment as your background tasks finish. – Naveed Jun 16 '15 at 17:50
  • One thread is in Picasso actually, the other is Android animation thread. And I do not need to dismiss animation. Problem is animation takes 500ms and sometimes I get response before animation finishes, and I do not want to update view before animation finishes. – Kandyzowana Papaja Jun 16 '15 at 17:51
  • You should be fine as long as you ensure that all access to your variable is synchronized. You can either use 2 booleans or a counter. – Naveed Jun 16 '15 at 18:20
  • Thank you for your advice. I was just wondering if that there might be some more elegant way to do this. – Kandyzowana Papaja Jun 16 '15 at 18:37
  • If you want to get fancy you could have a `BroadcastReceiver` and broadcast a message when a task is done. But the flags should work too. – nasch Jun 16 '15 at 19:22

0 Answers0