Is there a way to ask Android to return to a previous Activity within my application in the event that another Activity causes an unhandled exception to be thrown?
Is there a way to ask Android to return to a previous Activity within
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can try to use
Thread.setDefaultUncaughtExceptionHandlerto receive notification when any thread has died due to an unhandled exception. But I’m not sure about Dalvik implementation of that mechanism. It could be possible you would not be able to start another activity from theUncaughtExceptionHandleras the documentation says nothing about thread/process resurrection.Update
Ok. I tested it and now I’m sure you will NOT be able to return to a previous Activity using the above technique if your application thrown an exception in the UI thread. This happens because that exception would cause application main looper to exit and thus your application would not be able to process any further UI messages.
The only possible way I’ve found to achieve what you want is the following dirty-hack:
And register MyApplication in your AndroidManifest.xml:
The implementation of the
showPreviousActivity()method is up to you. One of the possible solution would be to keep track of the currently Activity instance in some ActivityTracker class and to call current activityfinish()method from theshowPreviousActivitycode.