I am using ActionBarSherlock to implement an UI using this powerful AB as a Menu. This is the menu layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/capturador"
android:icon="@drawable/ic_action_capturador"
android:showAsAction="always"
android:title="@string/capturador"/>
<item
android:id="@+id/preferencias"
android:icon="@drawable/ic_action_prefs"
android:showAsAction="always"
android:title="@string/preferencias"/>
</menu>
As you can see it has two items (capturador and preferencias)
So, what I wanna do -if it’s possible- is to re-use code. It’s not a big deal because I just have two more activities apart from Main activity but what if I had five? Should I overwrite in each activity the onOptionsItemSelected(MenuItem item) and onCreateOptionsMenu(Menu menu) methods?
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.capturador) {
startActivity(new Intent(this, Capturador.class));
} else if (item.getItemId() == R.id.preferencias) {
startActivity(new Intent(this, Ajustes.class));
}
return true;
}
Thanks in advance for your help and sorry if this question was already answered but I don’t really know which words I must use to try to find it (besides, English is not my mother tongue)
Just make an
BaseActivityin which you implement this code, and then let all yourActivitiesthat you want to have this 2 options menu, extend yourBaseActivity.