Android menu options provide the user with actions and other options for selecting from the action bar on the screen. Some of these actions are common to all actions for your application, so instead of creating them in each of your actions, you can create BaseActivity, which extends the Activity class and performs all your menu processing. You can then extend the base activity class in your application actions to get the same menu options.
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends BaseActivity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button nextActivity = (Button) findViewById(R.id.nextActivity); nextActivity.setOnClickListener(this); } }
Here is the BaseActivity class
import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class BaseActivity extends Activity{ @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.common_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {
Hope this helps you.
source share