处理加载活动线程的最佳实践
我有一个活动,在工作线程加载我的游戏时显示加载屏幕。加载工作线程时,会更新活动上的文本视图以显示其当前状态。完成后,它将关闭加载活动并开始实际的游戏活动。 工作线程是在 onCreate() 方法中创建的,并被赋予一个新创建的处理程序。
然而,当活动被破坏并重新启动(例如通过方向改变)时会发生什么?恕我直言, onCreate() 方法会在第一个工作线程仍在加载时创建一个新的工作线程,所以现在我有多个工作线程执行相同的操作。
防止这种情况并向工作线程通知新活动的最佳方法是什么(以便它可以发布其状态更新)。
I have an activity that displays a loading screen while a worker thread is loading my game. While loading the worker thread updates a textview on the activity to display its current status. When finished it will close the loading activity and start the actual game activity.
The worker thread is created in the onCreate() method and is given a newly created handler.
However what happens when the activity is destroyed and restarted e.g. by orientation change? IMHO the onCreate() methods would create a new worker thread while the first one is still loading, so now I got multiple worker threads doing the same.
What is the best way to prevent this and inform the worker thread about the new activity (so it can post its status updates).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你绑定了DroidFu库吗?它有一个名为 BetterAsyncTask 的类,似乎旨在解决您正在讨论的问题。您可以在这里找到该库( https://github.com/kaeppler/droid-fu )以及一篇关于您的问题和 droidfu 的文章( http://brainflush.wordpress.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/ - 请参阅“那篇文章”异步任务”部分)。
Have you tied the DroidFu library? It has a class named BetterAsyncTask that seems to be targeted at getting rid of the problem you're talking about. You can find the library here ( https://github.com/kaeppler/droid-fu ) and an article about your problem and droidfu here ( http://brainflush.wordpress.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/ - see the "That Dratted AsyncTask" section).