Where to declare onOptionsItemSelected for global use?
<p>I'm very new to Android development.</p>

<p>In my <code>MainActivity.java</code> file, I've declared an <code>onOptionsItemSelected(MenuItem menu)</code> method that allows the user to jump between the current <code>MainActivity.java</code> page and another page I created called <code>Settings.java</code>.</p>

<p>When the user goes to the <code>Settings.java</code> page, and they click on the <code>Home</code> option from the menu, nothing happens. I know this is because the <code>onOptionsItemSelected(MenuItem menu)</code> is only defined in the <code>MainActivity.java</code> class and not in the <code>Settings.java</code> class.</p>

<p>I'm overcoming this right now by copying the <code>onOptionsItemSelected(MenuItem menu)</code> from <code>MainActivity.java</code> into <code>Settings.java</code>. But this is very redundant.</p>

<p>Where should I be declaring methods that could be reused in different classes?</p>

<p><strong>MainActivity.java</strong></p>

<pre><code>public class MainActivity extends Activity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                showMsg("Settings Clicked");
                this.startActivity(new Intent(this, Settings.class));
                return true;
            case R.id.action_home:
                showMsg("Home clicked");
                this.startActivity(new Intent(this, MainActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void showMsg(String msg) {
        Toast toast = Toast.makeText(this.getBaseContext(), msg, Toast.LENGTH_LONG);
        toast.show();
    }
}
</code></pre>

<p><strong>Settings.java</strong></p>

<pre><code>public class Settings extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                this.startActivity(new Intent(this, Settings.class));
                return true;
            case R.id.action_home:
                this.startActivity(new Intent(this, MainActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
</code></pre>
