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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:31:33+00:00 2026-05-19T22:31:33+00:00

I’m trying to figure out how to include common pieces of code in multiple

  • 0

I’m trying to figure out how to include common pieces of code in multiple activities.

More specifically, I have a context menu that I would like to include in several activities.
I saw this, but just don’t understand how to extend to multiple activities.
http://developer.android.com/guide/topics/ui/menus.html

I have this set up as Menu.java

    public class Menu extends Activity{

    // bottom menus
    public static final int Menu1 = 1;
    public static final int Menu2 = 2;
    public static final int Menu3 = 3;
    public static final int Menu4 = 4;
    public static final int Menu5 = 5;
    public static final int Menu6 = 6;
    public static final int Menu7 = 7;


    // / Creates the menu items
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(0, Menu3, 0, "Create Profile").setIcon(
                this.getResources().getDrawable(R.drawable.ic_menu_add));
        menu.add(0, Menu5, 0, "Log In").setIcon(
                this.getResources().getDrawable(R.drawable.ic_menu_login));
        menu.add(0, Menu2, 0, "Settings").setIcon(
                this.getResources().getDrawable(R.drawable.ic_menu_preferences));
        menu.add(0, Menu4, 0, "About").setIcon(
                this.getResources().getDrawable(R.drawable.ic_menu_help));
        menu.add(0, Menu1, 0, "Report A Bug").setIcon(
                this.getResources().getDrawable(R.drawable.ic_menu_start_conversation));
        menu.add(0, Menu6, 0, "New Stuff").setIcon(
                this.getResources().getDrawable(R.drawable.ic_menu_view));
        return true;
    }



    private MenuItem add(int i, int menu32, int j, String string) {
        // TODO Auto-generated method stub
        return null;
    }



    // Handles item selections from preference menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case Menu1:
            startActivity(new Intent(this, Bug.class));
            return true;
        case Menu2:
            startActivity(new Intent(this, EditPreferences.class));
            return true;
        case Menu3:
            startActivity(new Intent(this, CreateAccount.class));
            return true;
        case Menu4:
            startActivity(new Intent(this, About.class));
            return true;
        case Menu5:
            startActivity(new Intent(this, Login.class));
            return true;
        case Menu6:
            startActivity(new Intent(this, NewAdditions.class));
            return true;
        }

        return false;
    }


}
  • 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-19T22:31:33+00:00Added an answer on May 19, 2026 at 10:31 pm

    if you want to add same functionality in more than 1 activity than create 1 common activity
    like BaseActivity and extend that activity will include that common functions in your inherited all activities

    for example i have called checklogin function , you can put your menu code here,

    public class BaseActivity extends Activity {
    
          @Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         settings = getSharedPreferences(PREFS_NAME, 0);
            if (IsFullScreen) {
               requestWindowFeature(Window.FEATURE_NO_TITLE);
               getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }
    
            this.CheckLogin();
         }
    
        // Check login function
        // Your menu code
    
      }
    

    now you can extend it in your activities

    public class MainScreen extends BaseActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    
        this.setContentView(R.layout.mainscreen);
    
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

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.