public < T > Observable < T > getAndObserve ( final String key , final TypeToken < T > typeTokenOfT , final T defaultValue ) { return observe ( key , typeTokenOfT , defaultValue ) // start observing . mergeWith ( Observable . defer ( new Callable < ObservableSource < ? extends T > > ( ) { // then start getting @ Override public ObservableSource < ? extends T > call ( ) throws Exception { return Observable . just ( get ( key , typeTokenOfT , defaultValue ) ) ; } } ) ) ; }
Gets value from SharedPreferences with a given key and type token as a RxJava Observable which can be subscribed If value is not found we can return defaultValue . Emit preference as first element of the stream even if preferences wasn t changed .