public class MainActivity extends AppCompatActivity { private Disposable disposable; private Observable<ModelSearchItems> observableCache; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState==null){ observableCache=RequestApi.getSearchItems() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()).cache(); } disposable=observableCache.subscribe(modelSearchItems -> { Log.e("MainActivity=onCreate", modelSearchItems.message.size() + ""); }); } @Override protected void onDestroy() { super.onDestroy(); disposable.dispose(); } } What am I doing wrong? On the Internet, I read an article about such an approach that the dude praised himself and everything worked for him ...
I get an error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.reactivex.disposables.Disposable io.reactivex.Observable.subscribe(io.reactivex.functions.Consumer)' on a null object reference MainActivity.onCreate(MainActivity.java:31)
if (savedInstanceState == null)something is not working andobservableCacheis stillnull. And why don't you just save it tosavedInstanceState? - kulikovman