Solved

How do I subclass(?) Android Menu?

Posted on 2011-03-08
4
533 Views
Last Modified: 2012-05-11
Hello,

This is more of a Java formatting question, but I was wondering how do I put my Menu Option Handler in another class and then refer to it elsewhere?

In other words, what do I need to pass to my subclass (is that even the right word to use?) and then how do I call that class in another class / activity?  Basically, this is a snippet of code that I would like to re-use in many classes / Activities, and if I modify it I would like it to change everywhere.

Attached is the code I would like to subclass(again, not sure if that is the right word).

Thanks!!!!
//handle menu 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.loading_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	// Handle item selection
        switch (item.getItemId()) {
        case R.id.option1:
        	
        	//do something in here that is relevant to 
                //the class that was calling it.  i.e.
                //stopping a video from playing etc.
        	
            
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
        
    }

Open in new window

0
Comment
Question by:stracqan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 150 total points
ID: 35070700
Can't you make those just static methods and refer to them with the class name.method from any place in your code?
0
 

Author Comment

by:stracqan
ID: 35070839
Would you mind posting an example please?
0
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 250 total points
ID: 35080622

What for_yan is suggesting is just creating a class, with only static methods. Example:


// Define a class and a static method on it:
public class MyMethods {

  public static void methodA(){
   //// Do stuff here
  };

}


/// To call that method anywhere, do this:

MyMethods.methodA();

You just need to add parameters that you will use to the method, thats it.

0
 

Author Closing Comment

by:stracqan
ID: 35085607
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This is an explanation of a simple data model to help parse a JSON feed
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question