public final class

BehaviorSubject

extends Subject<T>
java.lang.Object
   ↳ io.reactivex.Observable<T>
     ↳ io.reactivex.subjects.Subject<T>
       ↳ io.reactivex.subjects.BehaviorSubject<T>

Class Overview

Subject that emits the most recent item it has observed and all subsequent observed items to each subscribed Observer.

Example usage:

 // observer will receive all 4 events (including "default").
  BehaviorSubject<Object> subject = BehaviorSubject.createDefault("default");
  subject.subscribe(observer);
  subject.onNext("one");
  subject.onNext("two");
  subject.onNext("three");

  // observer will receive the "one", "two" and "three" events, but not "zero"
  BehaviorSubject<Object> subject = BehaviorSubject.create();
  subject.onNext("zero");
  subject.onNext("one");
  subject.subscribe(observer);
  subject.onNext("two");
  subject.onNext("three");

  // observer will receive only onComplete
  BehaviorSubject<Object> subject = BehaviorSubject.create();
  subject.onNext("zero");
  subject.onNext("one");
  subject.onComplete();
  subject.subscribe(observer);

  // observer will receive only onError
  BehaviorSubject<Object> subject = BehaviorSubject.create();
  subject.onNext("zero");
  subject.onNext("one");
  subject.onError(new RuntimeException("error"));
  subject.subscribe(observer);
   

Summary

Public Methods
static <T> BehaviorSubject<T> create()
Creates a BehaviorSubject without a default item.
static <T> BehaviorSubject<T> createDefault(T defaultValue)
Creates a BehaviorSubject that emits the last item it observed and all subsequent items to each Observer that subscribes to it.
Throwable getThrowable()
Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet.
T getValue()
Returns a single value the Subject currently has or null if no such value exists.
T[] getValues(T[] array)
Returns a typed array containing a snapshot of all values of the Subject.
Object[] getValues()
Returns an Object array containing snapshot all values of the Subject.
boolean hasComplete()
Returns true if the subject has reached a terminal state through a complete event.
boolean hasObservers()
Returns true if the subject has any Observers.
boolean hasThrowable()
Returns true if the subject has reached a terminal state through an error event.
boolean hasValue()
Returns true if the subject has any value.
void onComplete()
void onError(Throwable t)
void onNext(T t)
void onSubscribe(Disposable s)
Protected Methods
void subscribeActual(Observer<? super T> observer)
Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic.
[Expand]
Inherited Methods
From class io.reactivex.subjects.Subject
From class io.reactivex.Observable
From class java.lang.Object
From interface io.reactivex.ObservableSource
From interface io.reactivex.Observer

Public Methods

public static BehaviorSubject<T> create ()

Creates a BehaviorSubject without a default item.

Returns

public static BehaviorSubject<T> createDefault (T defaultValue)

Creates a BehaviorSubject that emits the last item it observed and all subsequent items to each Observer that subscribes to it.

Parameters
defaultValue the item that will be emitted first to any Observer as long as the BehaviorSubject has not yet observed any items from its source Observable
Returns

public Throwable getThrowable ()

Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet.

The method is thread-safe.

Returns
  • the error that caused the Subject to terminate or null if the Subject hasn't terminated yet

public T getValue ()

Returns a single value the Subject currently has or null if no such value exists.

The method is thread-safe.

Returns
  • a single value the Subject currently has or null if no such value exists

public T[] getValues (T[] array)

Returns a typed array containing a snapshot of all values of the Subject.

The method follows the conventions of Collection.toArray by setting the array element after the last value to null (if the capacity permits).

The method is thread-safe.

Parameters
array the target array to copy values into if it fits
Returns
  • the given array if the values fit into it or a new array containing all values

public Object[] getValues ()

Returns an Object array containing snapshot all values of the Subject.

The method is thread-safe.

Returns
  • the array containing the snapshot of all values of the Subject

public boolean hasComplete ()

Returns true if the subject has reached a terminal state through a complete event.

The method is thread-safe.

Returns
  • true if the subject has reached a terminal state through a complete event

public boolean hasObservers ()

Returns true if the subject has any Observers.

The method is thread-safe.

Returns
  • true if the subject has any Observers

public boolean hasThrowable ()

Returns true if the subject has reached a terminal state through an error event.

The method is thread-safe.

Returns
  • true if the subject has reached a terminal state through an error event

public boolean hasValue ()

Returns true if the subject has any value.

The method is thread-safe.

Returns
  • true if the subject has any value

public void onComplete ()

public void onError (Throwable t)

public void onNext (T t)

public void onSubscribe (Disposable s)

Protected Methods

protected void subscribeActual (Observer<? super T> observer)

Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic.

There is no need to call any of the plugin hooks on the current Observable instance or the Subscriber.

Parameters
observer the incoming Observer, never null