0

I don't know how to handle camera flash in my app. My app has a button which turn camera flash on and off, everything is OK and app working fine, even when pressing back button and coming back to app it works fine.

But when i press HOME button when app is running, and then come back to my app, when I press ON and OFF button it crashes(force close). I think the problem is in app's life cycle, but I don't know how to fix it.

in onStop() method, I use this code:

@Override
    protected void onStop() {
        super.onStop();

        if (camera != null) {
            camera.stopPreview();
            camera.release();
        }

and turn on the flash in onCreate() method using this code:

                Parameters p = camera.getParameters();
                if (!isLightOn) {
                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(p);
                    camera.startPreview();
                    isLightOn = true;
                                }
Mehrdad Salimi
  • 1,400
  • 4
  • 16
  • 31
  • 1
    instead of `onCreate`/`onStop` initialize your camera in `onResume` and release it and its surfaceView in `onPause`. Also, see http://stackoverflow.com/questions/8481402 in case you have similar issue – kiruwka Mar 26 '14 at 08:17
  • @kiruwka , that link's answers didn't help me, as you said I changed my code and initialized camera in `onResume()` and released it in `onPause()`, but still doesn't work, I am not familiar with `surfaceView` and don't know how to release `surfaceView`. – Mehrdad Salimi Mar 26 '14 at 08:50
  • with help of kiruwka and @Fox in socks and this link [link](http://stackoverflow.com/questions/14422190/refresh-activity-using-onresume/14422200#14422200) , my app work fine now, for more info check out the link. – Mehrdad Salimi Mar 26 '14 at 09:30

1 Answers1

2

You need to override onResume method of your activity. It could look like this:

@Override
protected void onResume() {
    super.onResume();

    if(camera == null){
        camera = getCamera();
        try {
            SurfaceHolder holder = cameraPreview.getHolder();
            camera.setPreviewDisplay(holder);

            cameraPreview.setCamera(camera);
            holder.addCallback(cameraPreview);

            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
eleven
  • 6,779
  • 2
  • 32
  • 52
  • sorry, I don't know what does your code do and how to use your code in my app, is there another way to handle flash in onResume() method? – Mehrdad Salimi Mar 26 '14 at 08:17
  • @MehrdadSalimi the problem not in flash, the problem in camera instance. When you call home button method `onStop` calls. Then when you launch your app method `onCreate` doesn't call. – eleven Mar 26 '14 at 08:23
  • I changed code and initiate camera in `onResume()` but still doesn't work, and I override `onPause()` method and use `camera.release()` in it, but still won't work. – Mehrdad Salimi Mar 26 '14 at 09:00
  • @MehrdadSalimi you can't just change code, you must understand lifecycle of `activity` and how to work with camera. And use logcat, it locates problem. "still won't work" doesn't make sense. – eleven Mar 26 '14 at 09:41
  • I understand life cycle of activity, my main problem was "how to open and release camera in activity life cycle, I finally found the answer here [link](http://stackoverflow.com/questions/14422190/refresh-activity-using-onresume/14422200#14422200), Thanks for your help.(and I will try to use logcat in future debugging) – Mehrdad Salimi Mar 26 '14 at 11:21