Android RxJava wrapper for updating and retrieving the saved state as any point in an activity/fragment or RxJava chain
Add jcenter() to the projects repositories build.gradle
repositories {
jcenter()
}In your module build.gradle file add the dependency
Current Version is in the gradle.properties file Here
dependencies {
compile "com.twistedequations.rx:rx-savestate:{{current version}}"
}If its giving an error about not finding a jar file use com.twistedequations.rx:rx-savestate:{{current version}}@aar instead
To obtain the previous state call the RxSaveState.getSavedState(activity) method, the observable returned emits the saved state as a bundle
downstream and completes. If there is no saved state then the observable just sends the onComplete event acting the same as Observable.empty()
RxSaveState.getSavedState(activity)
.map(newFunc1<Bundle, CharSequence>() {
@OverridepublicCharSequencecall(Bundlebundle) {
returnbundle.getCharSequence(STATE_KEY_TEXT);
}
})
.subscribe(newAction1<CharSequence>() {
@Overridepublicvoidcall(CharSequencecharSequence) {
Log.i(TAG, "State restored with string " + charSequence);
Toast.makeText(RxStateActivity.this, "State restored for string " + charSequence, Toast.LENGTH_SHORT).show();
}
});To save state you can use the RxSaveState.updateSaveState() function to update the state and add any new data. This can be called many times and any new values
will overwrite the previous one for a given key
RxView.clicks(button)
.map(newFunc1<Void, CharSequence>() {
@OverridepublicCharSequencecall(VoidaVoid) {
returneditText.getText();
}
})
.doOnNext(newAction1<CharSequence>() {
@Overridepublicvoidcall(finalCharSequencecharSequence) {
RxSaveState.updateSaveState(RxStateActivity.this,
newBundleAction() {
@Overridepublicvoidcall(Bundlebundle) {
bundle.putCharSequence(STATE_KEY_TEXT, charSequence);
}
});
}
})
.subscribe(newAction1<CharSequence>() {
@Overridepublicvoidcall(CharSequencecharSequence) {
Log.i(TAG, "State saved for string "+ charSequence);
Toast.makeText(RxStateActivity.this, "State saved for string "+ charSequence, Toast.LENGTH_SHORT).show();
}
});One very useful application is combine these functions with other data sources to easily combine restore state, disk cache and network loading
ObservablesavedStateObservable = RxSaveState.getSavedState(this) //save state and map to get the part of the state we need
.map(newFunc1<Bundle, List<Items>>() {
@OverridepublicList<Items> call(Bundlebundle) {
returnbundle.getParceable(STATE_KEY);
}
})
.switdhIfEmpty(networkObservable()) //Fail over the network observable if the saved data obervable is empty
.doOnNext(newAction1<List<Items>>() {
@Overridepublicvoidcall(finalList<Items> data) {
RxSaveState.updateSaveState(RxStateActivity.this,
newAction1<Bundle>() {
@Overridepublicvoidcall(Bundlebundle) {
bundle.putParceable(STATE_KEY, data); //Update state with the lastest data
}
});
}.subscribe(newAction1<List<Items>>() {
@Overridepublicvoidcall(List<Items> data) {
view.setData(data);
}
}Support has been added for rxjava2. To use RxJava2 import the dependency
dependencies {
compile "com.twistedequations.rx2:rx2-savestate:{{current version}}"
}