Solved

How do I subclass(?) Android Menu?

Posted on 2011-03-08
4
528 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
  • 2
4 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 150 total points
Comment Utility
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
Comment Utility
Would you mind posting an example please?
0
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 250 total points
Comment Utility

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
Comment Utility
Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now