content: package com.rxjava2.android.samples.ui.operators;
  
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  
  import com.rxjava2.android.samples.R;
  import com.rxjava2.android.samples.model.Car;
  import com.rxjava2.android.samples.utils.AppConstant;
  
  import androidx.appcompat.app.AppCompatActivity;
  import io.reactivex.Observable;
  import io.reactivex.Observer;
  import io.reactivex.disposables.Disposable;
  
  /**
   * Created by amitshekhar on 30/08/16.
   */
  public class DeferExampleActivity extends AppCompatActivity {
  
      private static final String TAG = DeferExampleActivity.class.getSimpleName();
      Button btn;
      TextView textView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_example);
          btn = findViewById(R.id.btn);
          textView = findViewById(R.id.textView);
  
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  doSomeWork();
              }
          });
      }
  
      /*
       * Defer used for Deferring Observable code until subscription in RxJava
       */
      private void doSomeWork() {
  
          Car car = new Car();
  
          Observable<String> brandDeferObservable = car.brandDeferObservable();
  
          car.setBrand("BMW");  // Even if we are setting the brand after creating Observable
          // we will get the brand as BMW.
          // If we had not used defer, we would have got null as the brand.
  
          brandDeferObservable
                  .subscribe(getObserver());
      }
  
      private Observer<String> getObserver() {
          return new Observer<String>() {
  
              @Override
              public void onSubscribe(Disposable d) {
                  Log.d(TAG, " onSubscribe : " + d.isDisposed());
              }
  
              @Override
              public void onNext(String value) {
                  textView.append(" onNext : value : " + value);
                  textView.append(AppConstant.LINE_SEPARATOR);
                  Log.d(TAG, " onNext : value : " + value);
              }
  
              @Override
              public void onError(Throwable e) {
                  textView.append(" onError : " + e.getMessage());
                  textView.append(AppConstant.LINE_SEPARATOR);
                  Log.d(TAG, " onError : " + e.getMessage());
              }
  
              @Override
              public void onComplete() {
                  textView.append(" onComplete");
                  textView.append(AppConstant.LINE_SEPARATOR);
                  Log.d(TAG, " onComplete");
              }
          };
      }
  
  
  }