I aim to shared the action bar in all activities. So I create a based class call "MyActionBarActivity" as follows"
public class MyActionBarActivity extends ActionBarActivity {
public final static int TL = Toast.LENGTH_LONG;
@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 super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "I am settings", TL).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
and other activities inherit from this class to share the action bar.
public class Activity1 extends MyActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
}}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}}
Is this a good way to implement the action bar?
In my opinion this is a fine way to apply the common action bars in more than one activity.
But inflating in separate activities also won't make much difference.
You can use either ways.