문제

I have inherited some code from an application that I need to modify (yuck). The activity in question implements SurfaceHolder.Callback and SensorEventListener. This activity is for a custom Camera.

The code works fine to autofocus, and take a picture. Once the picture is taken, I start another activity for result to crop the image. As soon as that activity is started, the SurfaceHolder.Callback for surfaceDestroyed is called (as expected). At this point the existing application releases the camera (as expected). However, when the new crop activity is launched, the original camera activity is stopped (stack trace: Instrumentation.callActivityOnStop->Activity.performStop).

Edit: Complete stack trace when the onStop of my activity is called:

CameraActivity.onStop() line: 784      
Instrumentation.callActivityOnStop(Activity) line: 1219
CameraActivity(Activity).performStop() line: 5186      
ActivityThread.performStopActivityInner(ActivityThread$ActivityClientRecord, ActivityThread$StopInfo, boolean, boolean) line: 3003     
ActivityThread.handleStopActivity(IBinder, boolean, int) line: 3052    
ActivityThread.access$1000(ActivityThread, IBinder, boolean, int) line: 139    
ActivityThread$H.handleMessage(Message) line: 1251     
ActivityThread$H(Handler).dispatchMessage(Message) line: 99    
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4918       
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
Method.invoke(Object, Object...) line: 511     
ZygoteInit$MethodAndArgsCaller.run() line: 1004
ZygoteInit.main(String[]) line: 771    
NativeStart.main(String[]) line: not available [native method] 

Because of this the camera activity is no longer active, and it doesn't receive the result from the crop activity.

Why does destroying the surface/releasing the camera cause the Camera Activity to stop? I must be missing something here...

도움이 되었습니까?

해결책 2

So, I did what I normally do in situations like this, and tried to simplify the problem. I created an entirely new project, following the guide on the android developer site: http://developer.android.com/guide/topics/media/camera.html#custom-camera

Once I got my new simple application to take a picture, I modified it to call the crop activity, and it worked without an issue.

The biggest different between the example application was that it used a separate CameraPreview class to implement the SurfaceHolder.Callback, where as my application implemented that interface directly in the Activity. I do not know if that was the true issue, but once I changed the code to do that, it seemed to work.

So unless someone can explain it better to me, the fix is to not implement SurfaceHolder.Callback in your activity.

다른 팁

Make sure that the request code passed to startActivityForResult is positive or your activity will not get the result. This is a weird part of the API that has bitten me before:

To quote docs: https://developer.android.com/reference/android/app/Activity.html#startActivityForResult

requestCode If >= 0, this code will be returned in onActivityResult() when the activity exits.

In general, onStop can be called any time your activity is not the foreground activity. This does not prevent receiving activity results. The Activity can be restarted in order to receive the result.

http://developer.android.com/guide/components/activities.html

Also, pay attention to task affinity and the back stack and make sure your crop activity is running as part of the same task as this will help to keep your activity from being stopped, but again no assurances.

http://developer.android.com/guide/components/tasks-and-back-stack.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top