In my Android apps, I used some kind of MVP (ModelViewPresenter). For the “Modification” request, I did the “Assignment” operation to the corresponding Presenter, which, in turn, makes a request for reinstallation, and as a parameter I send a callback with a user listener attached to it (implemented by the master). When the callbacks reach the onSuccess or onFailure , I call the corresponding Listener methods that call Presenter, and then the Activity: P methods
Now, if the screen is rotated when my activity is recreated, it joins the Leader. This is done using a custom implementation of the Android application, where it stores an instance of speakers and uses a map to restore the correct presenter according to the Activity class.
I don't know if this is the best way, maybe @pareshgoel's answer is better, but it worked for me: D
Examples:
public abstract interface RequestListener<T> { void onSuccess(T response); void onFailure(RetrofitError error); }
...
public class RequestCallback<T> implements Callback<T> { protected RequestListener<T> listener; public RequestCallback(RequestListener<T> listener){ this.listener = listener; } @Override public void failure(RetrofitError arg0){ this.listener.onFailure(arg0); } @Override public void success(T arg0, Response arg1){ this.listener.onSuccess(arg0); } }
Implement the listener somewhere on the presenter, and in the overrode methods, call the presenter method, which will make an Activity call. And call where you want the leader to start everything: P
Request rsqt = restAdapter.create(Request.class); rsqt.get(new RequestCallback<YourExpectedObject>(listener));
Hope this helps you.
LeoFarage Mar 14 '14 at 6:45 2014-03-14 06:45
source share