Reputation: 1391
I am launching another app through registerForActivityResult
(I guess the question makes sense with the deprecated startActivityForResult
as well, but I couldn't find a solution for that either). I can easily handle the result when the called app returns either with success or failure. But can I "catch" the exception of the called app when it crashes, and return control to the caller app?
My code:
[...]
val request = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
when (result.resultCode) {
Activity.RESULT_OK -> {
// Success, yay! Continue with data analysis
}
Activity.RESULT_CANCELED -> {
// Something bad happened! Do something about it
}
else -> {
// Something else unexpected happened! Log it, I suppose.
}
}
}
[...]
private fun startAnotherActivity(activity: MyActivity, intent: Intent, request: ActivityResultLauncher<Intent>): Boolean {
return try {
activity.withPermissions.checkSelfPermission(Manifest.permission.CAMERA) {
request.launch(intent)
}
true
} catch (e: ActivityNotFoundException) {
//This is of course catching if the other app is not present, but it's not going to catch anything if the called app crashes
} catch (e: Exception) {
//This is of course not catching if the other app crashes
}
}
So, is there a way to notice if another app crashes (for whatever reason) and not return to the Home screen?
Note: I don't know if it's relevant, but both apps, because of design requirements, have the <category android:name="android.intent.category.DEFAULT" />
tag in the intent-filter tag used to call them in the Manifest. Because actually, there's another app which is calling my app and my app is calling the third app, in chain, and only the first app appears in the tray, by design.
Upvotes: 0
Views: 373