How do I subclass(?) Android Menu?


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).

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

Open in new window

Who is Participating?
Dejan PažinConnect With a Mentor Head of SW DevelopmentCommented:

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:


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

for_yanConnect With a Mentor Commented:
Can't you make those just static methods and refer to them with the class name.method from any place in your code?
stracqanAuthor Commented:
Would you mind posting an example please?
stracqanAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.