title: RxJava onErrorResumeNext scheduler
body: <p>I have an observable which might fail with a special exception in which case I want to show a dialog with a retry button. I've seen <a href="https://stackoverflow.com/a/30840306/1348215">this</a> answer, but it doesn't quite do what I want. I wasn't able to use <code>retryWhen</code> to solve my problem, so instead I used <code>onErrorResumeNext</code>. If you can come up with a way to do the same with <code>retryWhen</code>, please tell.</p>
  
  <p>Right now I have this piece of code:</p>
  
  <pre><code>public Observable&lt;Order&gt; proceedWithOrdering(Activity activity) {
      return apiService.createOrder()
              .subscribeOn(Schedulers.io())
              .compose(applyRetryLogic(activity))
              .subscribeOn(AndroidSchedulers.mainThread())
              .observeOn(AndroidSchedulers.mainThread());
  }
  
  public &lt;T extends ApiResponse&gt; Observable.Transformer&lt;T, T&gt; applyRetryLogic(Activity activity) {
      return observable -&gt; observable
              .onErrorResumeNext(retry(observable, activity))
              .subscribeOn(AndroidSchedulers.mainThread());
  }
  
  public &lt;T&gt; Func1&lt;Throwable, ? extends Observable&lt;? extends T&gt;&gt; retry(Observable toRetry, Activity activity) {
      return throwable -&gt; {
          if (throwable instanceof NetworkException) {
              MaterialDialog dialog = retryDialog(activity);
              View retry = dialog.getActionButton(DialogAction.POSITIVE);
              View cancel = dialog.getActionButton(DialogAction.NEGATIVE);
              Observable&lt;Object&gt; retryClick = RxView.clicks(retry).map(o -&gt; {
                  dialog.dismiss();
                  return o;
              });
              Observable&lt;Object&gt; cancelClick = RxView.clicks(cancel).flatMap(o -&gt; {
                  dialog.dismiss();
                  return Observable.error(throwable);
              });
  
              dialog.show();
  
              return Observable.amb(retryClick, cancelClick)
                      .flatMap(o -&gt; toRetry.compose(applyRetryLogic(activity)));
          } else {
              return Observable.error(throwable);
          }
      };
  }
  </code></pre>
  
  <p>The problem is that the <code>call</code> inside the <code>retry</code> gets executed not on the main thread and it raises the <code>Can't create handler inside thread that has not called Looper.prepare()</code> exception.</p>
  
  <p>The question is - how do I force it to be executed on the main thread? As you can see, I have already tried doing <code>subscribeOn(AndroidSchedulers.mainThread())</code> right after both <code>compose</code> and <code>onErrorResumeNext</code> with no luck.</p>
  
  <p>I have tested my code using simple observables that don't operate on separate threads and it works fine.</p>