I am developing a flashlight App in which there is a normal flashlight in one activity and strobe light in one activity. Now I am acquiring the camera in onCreate of Flashlight activity. But when I am oving to strobe activity, i need to release the camera acquired by FlashLight activity. I dont want to release the camera in onPause of FlashLight activity because that would stop the camera even if user presses the home button. I want to release the camera only when user goes to strobe activity or else he exits the app by back button. Also I want to reacquire the camera if the user is coming back to flashlight activity from strobe activity. Is their anyway to do this.?
-
1What's the use case? Why would you want the camera to stay on when the user presses home? – Joffrey Apr 28 '14 at 08:27
-
Actually the torch should be on unless the user presses exit or turns it off.. – Aishvarya Jaiswal Apr 28 '14 at 18:28
-
How the user goes to `Strobe Activity` ? There should be some button or something right. You can release the camera when the user presses that button just before going to that activity! – Abhishek V Apr 29 '14 at 10:07
4 Answers
Other answers have told you not to do this and why. But to answer your question:
Keep a reference to the Camera
instance in a static member variable, preferably in a separate class, like this:
public class Globals {
public static Camera myCamera;
}
This variable is available to all of your activities as Globals.myCamera
.
Put the instance of Camera
that you get from calling Camera.open()
into Globals.myCamera
. This will be available to both activities. When you are ready to release the camera, call Globals.myCamera.release()
and then set Globals.myCamera
to null
to indicate that you no longer have control of the camera.

- 93,459
- 16
- 209
- 274
I want to release the camera only when user goes to strobe activity or else he exits the app by back button.
If you do not release the camera resources as soon as possible, the user will be unable to use camera from any other application. For example, if user of your app would press a home button, camera object would be left locked by your app. That would result in troublesome behaviour: for example, user failing to start a Camera application.
As official docs suggest:
Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause()
I want to release the camera only when user goes to strobe activity or else he exits the app by back button
If you do not release camera resources manually, they will not be released simply be pressing back button and "exiting" your app.
Also I want to reacquire the camera if the user is coming back to flashlight activity from strobe activity. Is their anyway to do this.?
Just connect to your camera in onResume(), and release resources in onPause().

- 3,307
- 22
- 33
I won't ask you to stop doing what you want or tell you about the official suggestions.
You can achieve that functionality by using a separate class for working with Camera, and declare all its functions and members as static.
- Whenever you switch to strobe activity just release the camera using that separate class's static function in onCreate of strobe activity.
- Do the same thing in
onDestroy()
of FlashLight activity, But if youfinish()
FlashLight activity when you switch to Strobe activity then you don't need to perform above step and your work will be done with this step only.

- 1,801
- 17
- 34
The best explanation for this is as given by David Wasser. We can create a global instance of the camera and then use it wherever required.
public class Globals { public static Camera myCamera; }
Although another workaround is to create buttons to move into each type of light and each and everyone having different classes. In such case we can leave the onpause on the camera as it is, and so it does not close the camera on home button press, but we can release the camera as soon as the user goes to the activity which contains buttons to select the camera light types. So the camera will be free always while selecting.

- 1,793
- 6
- 34
- 72