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
Solved

How do I subclass(?) Android Menu?

Posted on 2011-03-08
4
532 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ftp to port 21 4 53
drive pdf viewer 3 21
Selenium docs api java index 3 48
Running JavaFX on the Raspberry Pi 27 35
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is an explanation of a simple data model to help parse a JSON feed
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

792 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