Waiting for a function to finish execution and using the results

巧了我就是萌 提交于 2019-12-02 07:35:07
ngesh

When you create an instance of class B make sure it takes Context as an argument in its constructor. Then you can try using AsyncTask (Assuming you know it). Then put the time consuming function in doInBackground() and the function which waits for its values in onPostExecute()

Android development is event driven, you should normally not block threads waiting for a result (at least not the main/ui thread since your application then might be considered as not responding).

It's better to create a callback interface, and let the metod that takes a long time to execute, invoke a method on that inteface when the computation has been completed.

Btw, your post indicates that the computation already is asynchronous? You would otherwise have the result when the method has completed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!