Several activities using same listener

I got 4 activites that all include a xml-footer which contains 4 buttons (one for each activity). I would now like to setup onclicklisteners to these buttons (it's a self made menu in the footer).

The question is, how do I use listeners so that I can reuse code? I have two ideas:

  1. Create a class that implements onclicklistener and in every activity i would get the buttons and then create a new instance of the listener class and do button.setOnClickListener(onClickListener) The problem is that in the listener class, how would i check which button called the event? And how would I create an intent to start an activity, usually i would do: Intent intent = new Intent(FromActivity.this, ToAcitivty.class) But i don't have the reference to FromActivity.

  2. Create a base class that extends from activity and then the 4 activies will extend from the base class. I would then like to setup the listeners in the base class. The problem here is that i can't get the references to the buttons by doing Button button1 = (Button)findViewById(; button1 will be null. I haven't even called setEventView because this should be done in the activity not in the base class.

Any ideas?

Thank you


Same code is here:

    public class MyClass extends Activity implements View.OnClickListener{

    public void onClick(View v)
        Button clickedButton = (Button) v;
        switch (clickedButton.getId())
                Intent regIntent = new Intent(Home.this,Registration.class);
                startActivityIfNeeded(regIntent, 1);
                //Some code

(edited as the original first line is broken on code format.

Need Your Help

testing production config files in non-prodution environment

c# deployment production-environment

What is the best way to test production only config files before they are deployed to produciton?

How to open a new tab with PHP

php javascript html image new-window

I'm trying to open a new tab with Php, then display an image. I found openWindow() function but it applies to Javascript. I'm trying to use only Php.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.