4
\$\begingroup\$

In the past, I have used AsyncTasks to perform network requests for my Android app. I recently switched to RxJava to be able to cancel the requests when the user exits an activity. I have created a system to easily create new async tasks via RxJava which all cancel when a user exits an activity. I have attached some basic code that I hope somebody could give me some pointers on.

public class BaseActivity extends AppCompatActivity{
 ArrayList<OnDestroyListener> listeners = new ArrayList<>();
 @Override
 protected void onDestroy() {
 for(OnDestroyListener listener : listeners){
 listener.onDestroy();
 }
 super.onDestroy();
 }
 public void addOnDestroyListener(OnDestroyListener listener){
 listeners.add(listener);
 }
 public interface OnDestroyListener{
 void onDestroy();
 }
}
public class Tools{
 public static <E> void observe(Callable<E> callable, SingleSubscriber<E> subscriber, BaseActivity activity){
 final Subscription sub = Single.fromCallable(callable)
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(subscriber);
 if(activity == null){
 return;
 }
 activity.addOnDestroyListener(new BaseActivity.OnDestroyListener() {
 @Override
 public void onDestroy() {
 sub.unsubscribe();
 }
 });
 }
}
public class Endpoint{
 public void makeGetRequestAsync(final String url, final SingleSubscriber<String> subscriber, BaseActivity activity){
 Tools.observe(new Callable<String>() {
 @Override
 public String call() throws Exception {
 return makeGetRequestSync(url);
 }
 }, subscriber, activity);
 }
}
public class SampleActivity extends BaseActivity{
 Endpoint endpoint = new Endpoint();
 void onButtonClick(View v){ 
 endpoint.makeGetRequestAsync("https://someurl.com", new SingleSubscriber<String>() {
 @Override
 public void onSuccess(String result) {
 //Request success
 }
 @Override
 public void onError(Throwable error) {
 //Request failure
 }
 }, this);
 }
}

I know that I do not have a real coding problem, since this method has been working very well so far, but I just want to know if anything about this process is inherently wrong, as I am using it throughout my app.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 5, 2017 at 20:40
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I would start by trying to make a separate package for making the requests, something like a presenter(MVP pattern - MVP in Android).
Because, the way you have it now, it is hard to be tested and inside of the activity you have more logic, breaking a bit the SRP principle( SRP ).

Now, all it is left to do is to connect the activity and the presenter and that you can do using a callback.
Also, as a bonus, all the stuff you need, presenter, etc, you can add to a dagger module.

answered Jan 10, 2017 at 15:30
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.