RxJava - How to stop (and resume) a Hot Observable (interval)?
<p>I have the following Hot Observable:</p>

<pre><code>hotObservable = Observable.interval(0L, 1L, TimeUnit.SECONDS)
                          .map((t) -&gt; getCurrentTimeInMillis()))
</code></pre>

<p>However, I can't find a good way to stop it. I was able to partially solve this using <code>takeWhile</code> and a <code>boolean</code> flag (<code>runTimer</code>):</p>

<pre><code>Observable.interval(0L, 1L, TimeUnit.SECONDS)
          .takeWhile((t) -&gt; runTimer)
          .map((t) -&gt; getCurrentTimeInMillis()))
</code></pre>

<p>There are 2 things I don't like in this approach though:</p>

<ol>
<li>I must keep the flag <code>runTimer</code> around, which I don't want.</li>
<li>Once <code>runTimer</code> becomes <code>false</code>, the Observable simply completes, which means if I want to emit again I need to create a new Observable. I don't want that. I just want the Observable stop emitting items until I tell it to start again.</li>
</ol>

<p>I was hoping for something like this:</p>

<pre><code>hotObservable.stop();
hotObservable.resume();
</code></pre>

<p>That way I don't need to keep any flags around and the observable is always alive (it might not be emitting events though).</p>

<p>How can I achieve this?</p>