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 8473519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:22:25+00:00 2026-06-10T17:22:25+00:00

I have a buttonlistener, and when the user clicks the button I want to

  • 0

I have a buttonlistener, and when the user clicks the button I want to start a camera intent.
At the moment I have this:

public class ButtonListener implements View.OnClickListener 
{
private ArrayList<String> connectedItems;
private String identifier = null;
private Context context;
private EnteredValues enteredValues;

public ButtonListener(Context c, String identifier, ArrayList<String> connectedItems) {
    this.connectedItems = connectedItems;
    this.identifier = identifier;
    this.context = c;
}

public void onClick(View v) {
    if (identifier.equals(ButtonItem.takePhoto)) {
        MainActivity.takePhoto();
    }

}

Now I want to call a method in my mainActivity and there I want to start startActivityForResult, but I get an error that I can’t call startActivityForResult from a static method.

public class mainActivity extends Activity{
...
   public static void takePhoto(){
       startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE));
          break;

       //Here I get the error..
   }

}

What is the best practice to fix this sort of problem? pass a mainActivity object to my buttonListener or are there other options?

Many thx 🙂

  • 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-10T17:22:26+00:00Added an answer on June 10, 2026 at 5:22 pm

    Do not use Application context – as it will break your ActivityStack.
    I would recommend to add Activity parameter to this static method:

    public static void takePhoto(Activity activity, int requestCode){
       activity.startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), requestCode);
    }
    

    Than just pass Activity from your listener. You’ll have to pass Activity instance to it, instead of simple Context, as only Activity can make startActivityForResult() calls.

    So clickListener code will change as follows:
    public class ButtonListener implements View.OnClickListener
    {
    private ArrayList connectedItems;
    private String identifier = null;
    private Activity activity;
    private EnteredValues enteredValues;

    public ButtonListener(Activity activity, String identifier, ArrayList<String> connectedItems) {
        this.connectedItems = connectedItems;
        this.identifier = identifier;
        this.activity = activity;
    }
    
    public void onClick(View v) {
        if (identifier.equals(ButtonItem.takePhoto)) {
            MainActivity.takePhoto(activity, 100);
        }
    } 
    

    That is a good practice to make such static helpers to avoid creating intents and startActivities from different parts of your code.

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 class defined this way : public class Cartes extends JPanel {
have written this little class, which generates a UUID every time an object of
Have written all the code in a silverlight class library (dll) and linked this
I have two button's in the main menu. I invoking the camera when I
I have a Class, which contains inner class. I want to send value, which
Have deployed numerous report parts which reference the same view however one of them
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have a SQL problem, adding this model all works correctly, the problem is in
Have I in some way delete this Task or it is self-destroyed? Task.Factory.StartNew(() =>
Have such user_session model: class UserSession < Authlogic::Session::Base logout_on_timeout true # default if false

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.