Skip to content Skip to sidebar Skip to footer

Refresh Activity Using Onresume()

I have a Flashlight Activity. Normally it works fine but When I go to any other activity, it stop working! So I want to refresh the code when I back to the Flashlight Activity. I t

Solution 1:

You need to override onPause and onResume. In onPause, you need to release the Camera. In onResume, you need to re-request it. Camera doesn't like it if you try to hold it when you aren't the active activity.

public void onPause(){
    super.onPause();
    if(camera != null){
        camera.release();
        camera = null;
    }
}

public void onResume(){
    super.onResume();
    //Need to release if we already have one, or we won't get the camera
    if(camera != null){
        camera.release();
        camera = null;          
    }
    try {
        camera = Camera.open(); 
    }
    catch (Exception e){
    }

}

Post a Comment for "Refresh Activity Using Onresume()"