Reputation: 433
learning how to handling change orientation with RxJava and long tasks, read about retain fragment and some saving viewstate, but it's still unclear for me.
Imagine, we have network request via retrofit:
Retrofit retrofit = ApiClient.getClient(); //get retrofit instance
retrofit.create(ApiInterface.class).getImageList() // get list of image objects
.map(this::sleepDear)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::setupRecyclerView);
sleepDear()
public ImageResponse sleepDear(ImageResponse response) throws InterruptedException {
for (int i = 0; i < 5; i++) {
Log.d(TAG, "SLEEP");
TimeUnit.SECONDS.sleep(1);
}
return response;
}
setupRecyclerView()
public void setupRecyclerView(ImageResponse response) {
bar.setVisibility(View.GONE);
Log.d(TAG, "response size " + response.getImageList().size());
ImageAdapter adapter = new ImageAdapter(MainActivity.this, response.getImageList());
recyclerView.setAdapter(adapter);
}
sleepDear() method simulates long calculations (5 seconds) and after that setupRecyclerView() convert data from request to recyclerview, it drops down and start again, when I change orientation. What is best solution to fix? MVP can help me with separating view and model (retrofit request) layer. Thank you for answers.
Upvotes: 1
Views: 821
Reputation: 3914
you can take a look on this implementation, it creates a BaseActivity, that every Activity must extends if it needs to survive a configuration change.
it make use of Dagger2, you can find the ConfigPersistentComponent here, you use it simply by using the annotation @ConfigPersistent scope to annotate dependencies that need to survive for example Presenters.
Upvotes: 1