onActivityResult() & exception in child activity

Put your problem here if it does not fit any of the other categories.

onActivityResult() & exception in child activity

Postby KryshenAlexey » Thu Jun 24, 2010 9:52 am

Hello!

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.

It happens to me on NexusOne with "2.1-update 1" firmware

Thanks in advance
KryshenAlexey
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 24, 2010 9:12 am

Top

Re: onActivityResult() & exception in child activity

Postby anarche » Fri Jun 25, 2010 6:01 am

Solve the exception.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: onActivityResult() & exception in child activity

Postby KryshenAlexey » Fri Jun 25, 2010 7:39 am

anarche wrote:Solve the exception.

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.
KryshenAlexey
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 24, 2010 9:12 am

Re: onActivityResult() & exception in child activity

Postby KryshenAlexey » Fri Jun 25, 2010 2:06 pm

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.

Any thoughts ?
KryshenAlexey
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 24, 2010 9:12 am

Re: onActivityResult() & exception in child activity

Postby nicholas.hauschild » Sat Jun 26, 2010 4:22 am

So what is happening to your app right now??? Does it crash or what??
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: onActivityResult() & exception in child activity

Postby KryshenAlexey » Sat Jun 26, 2010 3:22 pm

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.

Is it clear now what the problem is ?

Thanks in advance
KryshenAlexey
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 24, 2010 9:12 am

Top

Re: onActivityResult() & exception in child activity

Postby anarche » Sat Jun 26, 2010 4:51 pm

Can't you catch the exception and exit back to the earlier state?
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: onActivityResult() & exception in child activity

Postby KryshenAlexey » Mon Jun 28, 2010 8:26 am

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.

Maybe you have other ideas ?

Thanks in advance
KryshenAlexey
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 24, 2010 9:12 am

Re: onActivityResult() & exception in child activity

Postby KryshenAlexey » Fri Jul 02, 2010 9:04 am

Are there any ideas ?
KryshenAlexey
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 24, 2010 9:12 am


Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Alexa [Bot], Google [Bot] and 23 guests