Search code examples
androidandroid-intentrx-javarx-androidactivity-finish

When finish Activity in BehaviorSubject's subscribe, The application is crashed


In 'subcribe' of 'BehaviorSubject', receive 'ArrayList' as a parameter to process data. If you try to pass the accepted data into the Intent and finish it, the app is stopped. What's the problem?

subject(requestData)
   .map(r->arrayList = r.getArrayList())
   .observeOn(Android.Schedulers.mainThread())
   .subscribe(r->{ finishWithData(r)});

private void finishWithData(r) {
   Intent intent = new Intent();
   intent.putExtra("array", r);
   setResult(Activity.RESULT_OK, intent);
   finish() // Here is Crash Point.
}

Solution

  • I would suggest getting Disposable from BehaviorSubject and dispose it in onDestroy of Activity.

    Disposable disposable;
    
    
    disposable = subject(requestData)
                  .map(r->arrayList = r.getArrayList())
                  .observeOn(Android.Schedulers.mainThread())
                  .subscribe(r->{ finishWithData(r)});
    
    @Override
    protected void onDestroy() {
        disposable.dispose();
        super.onDestroy();
    }