Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:33:11+00:00 2026-06-17T17:33:11+00:00

I have a Flashlight Activity. Normally it works fine but When I go to

  • 0

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 think refreshing using onResume() would help me best, But how to do it?

public class FlashLightActivity extends Activity {

//flag to detect flash is on or off
private boolean isLighOn = false;

private Camera camera;

private Button next1, next2;

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

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


        next1 = (Button) findViewById(R.id.ebtn28_answer);
        next1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), FullScreen.class);
                startActivityForResult(myIntent, 0);
            }

        });

    next2 = (Button) findViewById(R.id.buttonFlashlight);

    Context context = this;
    PackageManager pm = context.getPackageManager();

    // if device support camera?
    if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Log.e("err", "Device has no camera!");
        return;
    }

    camera = Camera.open();
    final Parameters p = camera.getParameters();

    next2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if (isLighOn) {

                Log.i("info", "torch is turn off!");

                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();
                isLighOn = false;

            } else {

                Log.i("info", "torch is turn on!");

                p.setFlashMode(Parameters.FLASH_MODE_TORCH);

                camera.setParameters(p);
                camera.startPreview();
                isLighOn = true;

            }

        }
    });

}
  }                                                              
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:33:12+00:00Added an answer on June 17, 2026 at 5:33 pm

    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){
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a flashlight application...Everything is working fine...except that whenever the flashlight is
Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()
have numerous tests working, but my actual test of some objects is failing and
This is the code from my MainActivity: package com.simple.flashlight; import android.os.Bundle; import android.app.Activity; import
I have this code in the main activity: package flash.light.pro; import android.os.Bundle; import android.app.Activity;
I have a Flashlight code that use two buttons (on_btn and off_btn) to turn
Have done quite a bit of searching for a guide (of any substance) for
I have a Flash/Flex object (Flashlight-VNC), which I would like to dynamically resize to
I'm trying to make a flashlight app for my iPhone. I have an iPhone
have defined a Java Interface with the annotation @WebService Compiled the code which went

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.