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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:35:57+00:00 2026-06-13T06:35:57+00:00

I have 2 activities, ActivityA and ActivityB , with 3 buttons each. I show

  • 0

I have 2 activities, ActivityA and ActivityB, with 3 buttons each. I show ActivityA on one connected device and ActivityB on the other connected device. The buttons can each be in 1 of 2 states but the states need to be reflected on both devices. I’m using a util class with static methods to receive button presses from both activities and send it to the service as well as update the state of the buttons in both activities upon the callback from a service.

Right now, each button calls a function in its corresponding activity from the xml. For example Button1‘s onClick looks like this:

android:onClick="button1Clicked"

That function (from its corresponding activity) calls a function in UtilClass and looks like this:

public void button1Clicked(View view) {
    UtilClass.button1Cliked();
}

I set an enum in UtilClass so I know which button to update (in both ActivityA and ActivityB) upon a callback from the service. It looks like this:

public class UtilClass {

public static Button buttonPressed = WfdOperationButton.NONE;

public enum Button {
    BUTTON1, BUTTON2, BUTTON3
}

public static void button1Cliked() {
        buttonPressed = Button.BUTTON1;
        //call asynctask to tell the service button1 was pushed by passing
        //buttonPressed
}

public static void button2Cliked() {
        buttonPressed = Button.BUTTON2;
        //call asynctask to tell the service button2 was pushed by passing
        //buttonPressed
}
...

My question is how can I use the same enum from UtilClass in ActivityA and ActivityB to consolidate the 3 buttonXClicked() functions in UtilClass? My attempted change in UtilClass is below:

public static void buttonCliked(Button inButton) {
        buttonPressed = inButton;
        //call asynctask to tell the service inButton was pushed by passing
        //buttonPressed
}

This doesn’t work though because I can’t use a non-static enum in ActivityA or ActivityB in a static function to pass in. But I need the enum to be non-static since I need to use it in the UtilClass.

I’m new to Android so if I’m doing anything else wrong, I’d be grateful if you pointed it out.

Thanks!

  • 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-13T06:35:58+00:00Added an answer on June 13, 2026 at 6:35 am

    These problems with static methods are perhaps evidence of O-O design issues. From a quick read it sounds like you’re trying to do too many things in one class, and it’s getting messy. Some ideas:

    Why are you defining your Button enum as a nested class here? You could just externalise it (i.e. move to its own file) and then the sharing problem goes away, assuming I understood you here.

    In general, if you are writing a helper class like UtilClass (though I doubt it’s actually necessary here), consider making everything non-static.

    To store state properly, consider one of the recommended Android methods. For simple things like this, Shared Preferences might be enough. If you really don’t want to do that, a stateful singleton object which references the enums as appropriate should do it. Then your call would look something like:

    public void button1Clicked(View view) {
        ButtonState.getInstance().setState(Button.Button1);
        // call wrapper for async call to say Button1 was pressed e.g.:
        serviceClient.sendButtonState(Button.Button1);
    }
    

    and

    class ButtonState {
        private static ButtonState instance = new ButtonState();
        private Button state
    
        public ButtonState getInstance() {
            return instance;
        }
    
        public void setState(Button value) {
            state = value;
        }
    
        public Button getState() {
            return state;
        }
    }
    

    or similar.

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

Sidebar

Related Questions

In my application I'm using some activities that can run each other. On resume
I have a TabActivity and inside 5 tabs with 5 activities each one. I
My Application have 5 activities(A1,A2,A3,A4,A5). Each activity have one text view and one button(B1,B2,B3,B4,B5).
I have two activities. One activity has the main game and the other activity
I have 2 activities. Activity A and Activity B. Both calling each other though
Newbie question... So I have two activities, Cherry and Apple, and each one has
I have 4 activities. A,B,C and D. From each activity user can start any
This is my situation. I have two activities: ONE and TWO. In TWO activity
Lets say we have a main class with 5 buttons, where when each clicked
I have a few dozen activities: 1-Main, 1-Animation, 10-Other's and it's sub-sets of more

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.