From 4622b0799bd9ae9240acc7960d4c3201ed9d2ae6 Mon Sep 17 00:00:00 2001 From: Lukasz Kopocinski Date: Sat, 15 Apr 2017 14:17:49 +0200 Subject: [PATCH] Forward Throwable to onError() when ActivityNotFoundException thrown --- app/src/main/java/app/OnPreResultActivity.java | 2 +- app/src/main/java/app/SampleActivity.java | 2 +- .../main/java/rx_activity_result2/HolderActivity.java | 9 ++++++++- .../src/main/java/rx_activity_result2/OnResult.java | 1 + .../java/rx_activity_result2/RxActivityResult.java | 10 ++++++++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/app/OnPreResultActivity.java b/app/src/main/java/app/OnPreResultActivity.java index 6beb410..ef5a617 100644 --- a/app/src/main/java/app/OnPreResultActivity.java +++ b/app/src/main/java/app/OnPreResultActivity.java @@ -26,7 +26,7 @@ protected void onCreate(Bundle savedInstanceState) { startPreForResult.setOnClickListener(v -> RxActivityResult.on(this) - .startIntent(new Intent(this, FirstActivity.class), (resultCode, data) -> + .startIntent(new Intent(this, FirstActivity.class), (resultCode, requestCode, data) -> Observable.just(Ignore.Get) .map(_I -> data.putExtra(EXTRA_PRE, "Do whatever you want with the data, but not with the UI"))) .subscribe(result -> { diff --git a/app/src/main/java/app/SampleActivity.java b/app/src/main/java/app/SampleActivity.java index ca58f7b..f917570 100644 --- a/app/src/main/java/app/SampleActivity.java +++ b/app/src/main/java/app/SampleActivity.java @@ -34,7 +34,7 @@ private void camera() { } else { result.targetUI().printUserCanceled(); } - }); + }, Throwable::printStackTrace); } private void showImage(Intent data) { diff --git a/rx_activity_result/src/main/java/rx_activity_result2/HolderActivity.java b/rx_activity_result/src/main/java/rx_activity_result2/HolderActivity.java index cbf6e0c..f252ed1 100644 --- a/rx_activity_result/src/main/java/rx_activity_result2/HolderActivity.java +++ b/rx_activity_result/src/main/java/rx_activity_result2/HolderActivity.java @@ -17,6 +17,7 @@ package rx_activity_result2; import android.app.Activity; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.IntentSender; import android.os.Bundle; @@ -52,7 +53,13 @@ protected void onCreate(Bundle savedInstanceState) { if (requestIntentSender.getOptions() == null) startIntentSender(requestIntentSender); else startIntentSenderWithOptions(requestIntentSender); } else { - startActivityForResult(request.intent(), 0); + try { + startActivityForResult(request.intent(), 0); + } catch (ActivityNotFoundException e) { + if (onResult != null) { + onResult.error(e); + } + } } } diff --git a/rx_activity_result/src/main/java/rx_activity_result2/OnResult.java b/rx_activity_result/src/main/java/rx_activity_result2/OnResult.java index e06a638..4050327 100644 --- a/rx_activity_result/src/main/java/rx_activity_result2/OnResult.java +++ b/rx_activity_result/src/main/java/rx_activity_result2/OnResult.java @@ -7,4 +7,5 @@ interface OnResult extends Serializable { void response(int requestCode, int resultCode, @Nullable Intent data); + void error(Throwable throwable); } diff --git a/rx_activity_result/src/main/java/rx_activity_result2/RxActivityResult.java b/rx_activity_result/src/main/java/rx_activity_result2/RxActivityResult.java index 5726cb8..b319640 100644 --- a/rx_activity_result/src/main/java/rx_activity_result2/RxActivityResult.java +++ b/rx_activity_result/src/main/java/rx_activity_result2/RxActivityResult.java @@ -117,6 +117,11 @@ public void response(int requestCode, int resultCode, Intent data) { subject.onNext(new Result<>(activity, requestCode, resultCode, data)); subject.onComplete(); } + + @Override + public void error(Throwable throwable) { + subject.onError(throwable); + } }; } @@ -141,6 +146,11 @@ public void response(int requestCode, int resultCode, Intent data) { //If code reaches this point it means some other activity has been stacked as a secondary process. //Do nothing until the current activity be the target activity to get the associated fragment } + + @Override + public void error(Throwable throwable) { + subject.onError(throwable); + } }; }