Solved

How do I subclass(?) Android Menu?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Trouble downloading purchased app on Samsung Galaxy S4 3 24
servlet example 17 32
mysql jsp example issue 32 37
Chrome and Firefox Java 5 32
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

778 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