最佳实践实施改造回调来重新创建活动?
我切换到改造,并试图了解正确的架构,使用它异步回调。
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...
使用奥托。 有很多的样本混合奥托和改造,例如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
或者阅读这篇文章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