Anuja Kothekar Anuja Kothekar - 12 days ago 6
Android Question

Custom CameraActivity is shown on screen even after calling finish()

I am having two activities:

HomeActivity
and
CameraActivity
.
HomeActivity
calls
CameraActivity
using intent with flag
Intent.FLAG_ACTIVITY_NEW_TASK
and
startActivityForResult
.
CameraActivity
is having a fragment which opens a Custom Camera. I have given a button to capture image. If user clicks on button, capture callback of camera is called and in
onPictureTaken(byte[] data)
I am writing data array in image file and finishing activity by using
getActivity().finish()
and returning path of image file to
HomeActivity
and showing it on UI.

Now the problem is:

When I am calling
getActivity().finish()
from fragment in
CameraActivity
, it calls
onActivityResult
of
HomeActivity
. Path is shown on
TextView
. Logs are also printed properly from
HomeActivity
. But on Screen Camera runs. Every time it requires back button to be pressed to end
CameraActivity
.
Suppose I am capturing image for once but because on screen camera is still running I tried to capture it twice, control returned on
HomeActivity
but
onActivityResult()
is not called for second capture.

Answer

As I was using fragments in Activity, when configuration changes occured, Activity is restarted and new instance of fragment is attached because of which it continued to showing unfinished fragment on screen. I solved this problem by detaching fragment on configuration change.