Skip to content Skip to sidebar Skip to footer

Flashlight App Getting Closed In Background

I am developing a flashlight app and the thing is that the app is working as expected, but not when 'home' or 'back' key is pressed. i want my app to keep the torch light on even w

Solution 1:

Got solution.

What you should do is, write all the code like following in onBackPressed. I merge two methods code in one method.

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    myparas = mycamera.getParameters();
    myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
    mycamera.setParameters(myparas);
    mycamera.stopPreview();
    flashon = false;

    if (mycamera != null) {
        mycamera.release();
        mycamera = null;
    }
    Log.d("Camera","Back Pressed");
}

And remove all the code from onStop() and onPause() method. Because when you press Home key, it calls first onPause() and then onStop(). I implement your code and worked fine for me.


Solution 2:

Change you onPause() to this and also onStop() if you need to

@Override
protected void onPause() {
    super.onPause();
}

and add this

@Override
protected void onBackPressed() { 
    super.onBackPressed();
    // on back turn off the flash
    myparas = mycamera.getParameters();
    myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
    mycamera.setParameters(myparas);
    mycamera.stopPreview();
    flashon=false;
}

Solution 3:

Here I Modified the code set !

It worked for me ! Thanks

  @Override
protected void onDestroy() {
    super.onDestroy();
}

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

    // on pause turn off the flash
  //  turnOffFlash();
}

@Override
protected void onRestart() {
    super.onRestart();
}

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

    // on resume turn on the flash
    if(hasFlash)
        turnOnFlash();
}

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

    // on starting the app get the camera params
    getCamera();
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    isFlashOn = false;

    if (camera != null) {
        camera.release();
        camera = null;
    }
    Log.d("Camera","Back Pressed");
}

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

    // on stop release the camera
 //   if (camera != null) {
 //       camera.release();
  //      camera = null;
   // }
}

Post a Comment for "Flashlight App Getting Closed In Background"