I have faced an issue with startActivityForResult() and onActivityResult(). The problem is that onActivityResult() is not invoked if child activity is exit unexpectedly due to the unhandled exception. It says in documentation for onActivityResult() that RESULT_CANCELED will be used if child activity crashed but it is not happened in real.

Is there any workaround for this issue ? Logic of any application with startActivityForResut() is potentially broken if it will not receive expected onActivityResult() or being able to detect child activity crash in any other way.

The child activity is not mine and it is impossible. In my case it was built-in Camera activity to capture the picture. So in the case of any third-party activities it is not a general solution.It may be possible to implement workarounds for some specific cases but you can't avoid all bugs/issues in third-party activities in such way and only right way is to be ready to handle all possible scenarios of child activity termination.

I have recheck the documentation again and it states explicitly that if child activity is crashed then parent activity would receive the RESULT_CANCELED code in onActivityResult(). It seems to be bug in Android because it is not a case.

nicholas.hauschild wrote:So what is happening to your app right now??? Does it crash or what??

The problem is that the logic of application is broken due to it. At some moment application want to get picture, perform some operation with it and then use result for its own purposes. The logic of application is very straightforward: start Camera activity, wait for result, go further depending the Camera activity result (normal or canceled). Need to say that my application is not just an plain activity but main application code runs in the separate thread and activity is used only to "connect" Android system to this thread (events retranslating, runs other activities like the Camera one and so on). And this main code in fact implements some kind of "state machine". After requesting picture from the camera this state machine switches to the "Awaiting Picture" state and only way to leave it is a get result of Camera activity (either normally picture taken or canceled/crashed). Because onActivityResult() is not invoked under the described circumstances the application logic is broken and it can't leave the "Awaiting Picture" state.

anarche wrote:Can't you catch the exception and exit back to the earlier state?

It is impossible. You can't catch exceptions in the child activities from the parent one even if both is yours. In the case of third-party activities like the Camera they are runs in the another VM in separate process at all.

The only way I realized to fight the issue is a check for the onActivityResult() occur before onResume() in the parent activity. Documentation for onActivityResult() states than it will occur immediately before onResume(). So if after we call startActivityForResult() and get onResume() without onActivityResult() prior it we consider child activity being crashed and do things just like we are receive the RESULT_CANCELED directly. It is ugly hack and I do not like it extremely but no other way I was able to realize to resolve the issue.