Snake
Snake

Reputation: 14648

How to know which activity clicked the button?

I have 10 activites that extends baseActivity. My BaseActivity basically has a help menu icon. When the user press the help menu Icon, I should display the help menu for that activity. How can I know which activity called the help menu so I know which xml resource to display

public class BaseActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.help_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.help_menu_item:
                    displayHelpMenu();          
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    protected void displayHelpMenu(){

             // I will load the xml to the textview and display it in dialog. How can I know which xml resource to load

    }
}

Upvotes: 2

Views: 61

Answers (2)

bladefury
bladefury

Reputation: 815

how about make your BaseActivity abstract and add a getMenuResource method?

public abstract class BaseActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.help_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.help_menu_item:
                    displayHelpMenu();          
                return true;

            default:
                return super.onOptionsItemSelected(item);
        } 
    }

    protected void displayHelpMenu(){

             // I will load the xml to the textview and display it in dialog. How can I know which xml resource to load
        int menuResourceId = getMenuResourceId();
        // do the rest
    }

    public abstract int getMenuResourceId();
}

Upvotes: 1

Amulya Khare
Amulya Khare

Reputation: 7698

One way to find out the Activity is to use instanceOf. Something like this:

protected void displayHelpMenu() {
    // I will load the xml to the textview and display it in dialog.
    // How can I know which xml resource to load

    if(this instanceOf DerivedActivity.class) {
        // load XML file
    }
    else if(...) {
    ...
    }
}

However, in my opinion, while this solves your problem, this is not the correct way of doing things. In OOP when you end up with a situation like this, you should make use of method overriding.

The base class defines general functionality of loading the resource, while the derived class should provide the name of the file to load. In other words, the base class should not know about the derived class. A better solution would be:

protected void displayHelpMenu() {

    // ask each child class for the help menu resource file
    int resId = getHelpMenuResource();

    // write code to load XML file with resId
    ...
    ...
}

protected int getHelpMenuResource() {
    return defaultMenuId;
}

Now in your derived class, @Override the getHelpMenuResource() method and return resource id specific to that activity.

Upvotes: 3

Related Questions