title: Executing a network request every X seconds, how to pause/resume?
body: <p>Previously, I was using a <code>Runnable</code> with a <code>Handler</code>, and executing <code>.postDelayed(runnable, delay)</code> with a delay of 3 seconds, to send a network request to an api, to update coordinates on a map. I thought this was a great opportunity to try and get some Rx in. I switched the Retrofit call to return an <code>Observable</code>, and use the <code>.interval()</code> operator to implement the repeated calls. Heres my current code:</p>
  
  <pre><code>Observable.interval(3000, TimeUnit.MILLISECONDS)
              .flatMap(new Func1&lt;Long, Observable&lt;IssResponse&gt;&gt;() {
                  @Override
                  public Observable&lt;IssResponse&gt; call(Long aLong) {
                      Log.d("api", "inside the call method: " + aLong.toString());
                      return issService.getLocation();
                  }
              }).subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Subscriber&lt;IssResponse&gt;() {
                  @Override
                  public void onCompleted() {
                      Log.d("api", "rx comleted");
                  }
  
                  @Override
                  public void onError(Throwable e) {
                  }
  
                  @Override
                  public void onNext(IssResponse issResponse) {
                      Log.d("api", "response: " + issResponse.getIssPosition().getLatitude().toString());
                  }
              });
  </code></pre>
  
  <p>This seems to function as expected, and returns a result every 3 seconds. Now what I would like to do, is "pause" and then afterwards resume these requests, as with the Android lifecycle, I dont want to keep executing requests if the user switches to a different fragment so that the map isnt even visible.</p>
  
  <p>With the <code>Handler</code> and <code>Runnable</code>, I figured I could call <code>handler.removeCallbacks(runnable)</code> in the <code>onPause()</code> method, which seemed to work alright for pausing the requests. I didnt get around to implementing the resume functionality using the <code>Handler</code> and <code>Runnable</code> method as I decided to switch to Rx. So how could this functionality be implemented with Rx? How could I pause and later resume the requests using RxJava?</p>
  
  <p><strong>EDIT:</strong> found a way to pause using <code>Observable.takeWhile(boolean)</code> and toggling the boolean. Now I just need a way to resume..</p>