In this sample Android OCR application, that uses com.google.android.gms:play-services-vision
, there is a OcrDetectorProcessor.receiveDetections()
method which gets repeatedly called. In that method, while the user points the camera at things in the environment, we have access to whatever text is detected.
If the user presses the back
button, the process ends, but I would like to have the process end when a certain condition occurs within the receiveDetections()
method.
I tried calling mParentActivity.onBackPressed()
, but that threw an exception from super.onBackPressed()
:
OpenCameraSource: Exception thrown from receiver. java.lang.IllegalStateException: Must be called from main thread of fragment host
So although the exception is caught and the app continues to run, calling onBackPressed()
is not the right approach. What is the right way to programatically back-out of receiveDetections()
?
Instead of:
mParentActivity.onBackPressed();
Use:
mParentActivity.runOnUiThread(new Runnable() { public void run() { mParentActivity.onBackPressed(); } });