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

  • Home
  • SEARCH
  • 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 6818263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:09:25+00:00 2026-05-26T21:09:25+00:00

In various activities I have very similar methods. For example: @Override public void onClick(View

  • 0

In various activities I have very similar methods.

For example:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.ibHome:
        Intent menuIntent = new Intent(v.getContext(),Menu.class);
        startActivity(menuIntent);
        break;
    }

}

and

@Override
public void onClick(View v) {
     /**  */

    //
    switch(v.getId()){
        case R.id.bNew:
            Intent newSwimmerIntent = new Intent(v.getContext(),NewSwimmer.class);
            startActivity(newSwimmerIntent);
            break;
case R.id.ibHome:
            Intent menuIntent = new Intent(v.getContext(),Menu.class);
            startActivity(menuIntent);
            break;

is there a way (which I assume would be using inheritance) to prevent the menuIntent being explicitly stated in each each class?

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-05-26T21:09:26+00:00Added an answer on May 26, 2026 at 9:09 pm

    You could use inheritance, but that actually may not be the best choice, especially if the two activities are not directly related in an “Is-A” relationship.

    In this case, you’re probably better off declaring a new class that implements OnClickListener interface. You can then instantiate that class and bind it to your buttons’ click events wherever they’re used.

    Here’s an example:

    public class HomeButtonHandler implements OnClickListener{
    
            @Override
            public void onClick(View v) {
                Intent menuIntent = new Intent(v.getContext(),Menu.class);
                v.getContext().startActivity(menuIntent);            
    
            }
    
        }
    

    And in your activities, you can just bind the HomeButtonHandler to the onClickListner of the button:

    homeButton.setOnClickListener(new HomeButtonHandler());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an android app that has various activities. for example there is a
What various methods and technologies have you used to successfully address scalability and performance
I have a function to display a notification, which I call from various activities.
I have build a service to broadcast a timer data to various activities. Usually
Various online services have different values for maximum year of expiry, when it comes
At various times in my career I have encouraged staff I worked with and/or
Here's the deal. I have an application with three tabs. Through various interactions with
Is there any way to share the same object of the View across various
I'm starting an Android app which will have multiple Activities. It will be using
I have an application that is driven by a configuration XML: various app properties

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.