Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android manage multi request rxJava on rotation device

I'm using MVVM on android application and i want to manage requests and rxJava on device rotation, how can i disable request after rotation device and countinue from last request?

this is my simple code to know how can i doing that, but i can't find any document and sample code about it

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
    ...
    Observer<String> myObserver = new Observer<String>() {
        @Override
        public void onError(Throwable e) {
            // Called when the observable encounters an error
        }

        @Override
        public void onComplete() {

        }

        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(String s) {
            // Called each time the observable emits data
            Log.e("MY OBSERVER", s);
        }
    };

    Observable.just("Hello").subscribe(myObserver);
}

I'm using latest version of rxJava

like image 498
tux-world Avatar asked Mar 19 '17 14:03

tux-world


1 Answers

Handling rotation is a cool challenge in Android. There're a few ways to do that.

1- Services: You can use a service and handle your network requests or other background operations in service. Also with Services, you'll seperate your business logic from ui.

2- Worker Fragment: Worker fragment is a fragment instance without a layout. You should set your worker fragment's retainInstanceState to true. So you'll save your fragment from orientation change and will not lose your background operations.

Why Worker Fragment? If you set retainInstanceState true to a fragment with layout, you'll leak views.

If you're using MVVM you can implement ViewModel as a Worker Fragment which as setRetainInstanceState = true

3- Global Singleton Data Source: You can create a global singleton data source class which handles your operations in an independent scope from Activity / Fragment lifecycle in your application.

4- Loaders: Loaders can recover state from orientation changes. You handle your operations with loaders but they are designed to load data from disk and are not well suited for long-running network requests.

Extra: You can use Path's Priority Job Queue to persist your jobs: https://github.com/path/android-priority-jobqueue

Edit: You can check my repo for handling device rotation without using Google's new architecture components. (As an example of Worker Fragment which i pointed in my answer.) https://github.com/savepopulation/bulk-action

like image 91
savepopulation Avatar answered Oct 22 '22 03:10

savepopulation