最佳实践实施改造回调来重新创建活动? [英] Best practice to implement Retrofit callback to recreated activity?

查看:183
本文介绍了最佳实践实施改造回调来重新创建活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我切换到改造,并试图了解正确的架构,使用它异步回调。

I'm switching to Retrofit and trying to understand proper architecture for using it with async callbacks.

例如我有一个接口:

interface RESTService{
    @GET("/api/getusername")
    void getUserName(@Query("user_id") String userId, 
                     Callback<Response> callback);
}

和我跑这从主要活动:

RestAdapter restAdapter = new RestAdapter.Builder()
        .setServer("WEBSITE_URL")     
        .build();
RESTService api = restAdapter.create(RESTService.class);
api.getUserName(userId, new Callback<Response> {...});

然后用户旋转设备,我还新创建活动...什么是发生在这里?我怎样才能得到响应新的活动(我假设的背景下,API调用会比第一个活动的生命执行长)。也许我必须使用回调还是什么的静态实例?请告诉我正确的方式......

Then user rotates the device and I have newly created activity... What was happen here? How can I get response to the new activity (I assume that api call in background will execute longer than first activity life). Maybe I must use static instance of callback or what? Please show me the right way...

推荐答案

使用奥托。 有很多的样本混合奥托和改造,例如<一个href="https://github.com/pat-dalberg/ImageNom/blob/master/src/com/dalberg/android/imagenom/async/FlickrClient.java">https://github.com/pat-dalberg/ImageNom/blob/master/src/com/dalberg/android/imagenom/async/FlickrClient.java

Use otto. There are a lot of samples to mix otto and retrofit, for example https://github.com/pat-dalberg/ImageNom/blob/master/src/com/dalberg/android/imagenom/async/FlickrClient.java

或者阅读这篇文章<一个href="http://www.mdswanson.com/blog/2014/04/07/durable-android-rest-clients.html">http://www.mdswanson.com/blog/2014/04/07/durable-android-rest-clients.html 它回答几乎所有的问题

Or read this post http://www.mdswanson.com/blog/2014/04/07/durable-android-rest-clients.html It answers on almost all questions

这篇关于最佳实践实施改造回调来重新创建活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆