title: Getting variables "into" RxJava chain for use later
body: <p>I am using RxJava on Android to perform a login operation.</p>
  
  <p>I need to pass in a username, password and a boolean flag. The username and password and sent to a server for verification and once a response is returned I need to use the flag to determine what to do next.</p>
  
  <p>Since the login operation is asynchronous, I want to ensure that when the response returns that I will still have access to the username, password and flag that I passed in at the beginning.</p>
  
  <p>Here is the initial way I coded this up that I believe has problems:</p>
  
  <pre><code>    public Observable&lt;Result&gt; execute1(final String username, final String password, final boolean shouldSaveUsername) {
      return mLoginNetwork
              .loginWithCredentials(username, password)
              .map(new Func1&lt;Response&lt;Void&gt;, LoginObject&gt;() {
                  @Override
                  public LoginObject call(Response&lt;Void&gt; response) {
                      if (!response.isSuccessful()) {
                          Exceptions.propagate(new HttpException(response));
                      }
  
                      return new LoginObject(username, password, shouldSaveUsername);
                  }
              })
              .doOnNext(new Action1&lt;LoginObject&gt;() {
                  @Override
                  public void call(LoginObject loginObject) {
                      if (loginObject.shouldSaveUsername) {
                          saveUsername(username);
                      }
                  }
              })
              .flatMap(new Func1&lt;Entitlement, Observable&lt;Result&gt;&gt;() {
                  @Override
                  public Observable&lt;Result&gt; call(LoginObject loginObject) {
                      return mNetwork
                              .fetchSomething();
                  }
              });
  }
  </code></pre>
  
  <p>When I call execute1() it returns an Observable which I cache and then subscribe to. If an Android configuration change occurs I unsubscribe from the Observable but keep it in a cache. Once the configuration change is complete I take the Observable out of the cache and resubscribe to it. When I resubscribe the loginWithCredentials call would need to be made again, but when it returns the username, password and boolean flag would no longer exist and therefore I wouldn't be able to use them in my chain which is a problem. </p>
  
  <p>So, how to solve this issue?</p>
  
  <p>I need a way for the input data to the Observable to become part of the Observable so that when I cache the Observable the input data is also cached.</p>
  
  <p>Here is a proposed solution below:</p>
  
  <pre><code>    public Observable&lt;Result&gt; execute2(String username, String password, boolean shouldSaveUsername) {
      return Observable
              .just(new LoginData(username, password, shouldSaveUsername))
              .flatMap(new Func1&lt;LoginData, Observable&lt;LoginData&gt;&gt;() {
                  @Override
                  public Observable&lt;?&gt; call(final LoginData loginData) {
                      return mLoginNetwork
                              .loginWithCredentials(loginData.getUsername(), loginData.getPassword())
                              .map(new Func1&lt;Response&lt;Void&gt;, LoginData&gt;() {
                                  @Override
                                  public LoginData call(Response&lt;Void&gt; response) {
                                      if (!response.isSuccessful()) {
                                          Exceptions.propagate(new HttpException(response));
                                      }
                                      return loginData;
                                  }
                              });
                  }
              })
              .doOnNext(new Action1&lt;LoginData&gt;() {
                  @Override
                  public void call(LoginData loginData) {
                      if (loginData.shouldSaveUsername) {
                          saveUsername(username);
                      }
                  }
              })
              .flatMap(new Func1&lt;LoginData, Observable&lt;Result&gt;&gt;() {
                  @Override
                  public Observable&lt;Result&gt; call(LoginData loginData) {
                      return mNetwork
                              .fetchSomething();
                  }
              });
  }
  </code></pre>
  
  <p>What I'm attempting to do is to make the input data part of the stream right away by using Observable.just() to take the input data and make it into an Observable and then let the rest of the downstream operations receive it as an input. I assume that if I now cache the observable and resubscribe later that the input data is now embedded in my observable and can be accessed in any of the operators later. </p>
  
  <p>Have I solved my problem in my proposed solution in a "normal" RxJava / functional way? Are there better ways to approach this problem?</p>