I am using the same tabBar across multiple Activities. And since there are extensive logic involved for onOptionsItemSelected
, I want to write the relevant methods once and then reuse them. Hence I am deciding to created a super class called CustomActionBarActivity
and then have the children activities extend it. One particular problem I need help with is how can I tell which child has caused the onOptionsItemSelected
to be called? To elucidate, I will present the general code and then a failed attempt at a solution.
Here is the general code
public class CustomActionBarActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.tab_dog:
startActivity(new Intent(this, DogActivity.class));
return true;
case R.id.tab_cat:
startActivity(new Intent(this, CatActivity.class));
return true;
case R.id.tab_mouse:
startActivity(new Intent(this, MouseActivity.class));
return true;
case R.id.tab_goose:
startActivity(new Intent(this, GooseActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Failed attempt
If I try, for instance,
case R.id.tab_dog:
if(!(this instanceof DogActivity))
startActivity(new Intent(this, DogActivity.class));
then I get a compile error such that CustomActionBarActivity is not compatible with DogActivity. Thanks for any help you can provide.