大佬教程收集整理的这篇文章主要介绍了使用AndroidAnnotations时@Background和屏幕旋转,如何确保收到回调?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
与AsyncTask一样,@ BACkground不会处理您的活动的任何生命周期更改.因此,如果您调用@BACkground方法然后旋转屏幕,那么无论如何,都会在调用它的实例上执行@BACkground代码.如果将@BACkground置于属于该活动的方法上,并依次调用@UiThread方法,则可能会在@L_674_15@的活动实例上调用@UiThread方法(如果发生配置更改).
在Android中,在Loaders之前,处理它的常用方法是使用AsyncTask,在onRetainNonConfigurationInstance()中保留对这些任务的引用,并在配置更改后将它们重新绑定到新活动.
在最新版本中,AndroidAnnotations提供@NonConfiguration实例注释,可以与@EBean / @Bean和@BACkground结合使用以实现相同的效果.
@EActivity public class MyActivity extends Activity { // Using @NonConfigurationInstance on a @Bean will automatically update the context ref on configuration changes (if the bean is not a singleton) @NonConfigurationInstance @Bean MyBACkgroundTask task; @Click void myButtonClicked() { task.doSomethingInBACkground(); } void showResult(MyResult result) { // do something with result } } @EBean public void MyBACkgroundTask { @RootContext MyActivity activity; @BACkground void doSomethingInBACkground() { // do something MyResult result = XXX; updateUI(result); } // Notice that we manipulate the activity ref only from the UI thread @UiThread void updateUI(MyResult result) { activity.showResult(result); } }
我认为我们可以提供更好的解决方案,但是有很多不同的用例,我们需要考虑所有这些.所以现在,@ BACkground有一个非常简单的行为,我不想改变它.但是,我们可以引入具有高级“线程生命周期”行为的新注释.
Thanx到“Pierre-Yves Ricau”通过googlegroup为androidAnnotations提供这个答案.希望这会帮助那些可能遇到类似问题的人.
以上是大佬教程为你收集整理的使用AndroidAnnotations时@Background和屏幕旋转,如何确保收到回调?全部内容,希望文章能够帮你解决使用AndroidAnnotations时@Background和屏幕旋转,如何确保收到回调?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。