1

While working with my application, I had to come to come across a situation where I supposed to use menu which has to get displayed in entire application when user click menu button.
So I used following code in Default activity but then realized that menu is displaying in that activity but not in all.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.del_boy_menu, menu);
    //below comented code for changung dynamically
    // MenuItem bedMenuItem = menu.findItem(R.id.home);
    // bedMenuItem.setTitle("title changed");
    // System.out.println("onCreate executed");
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    System.out.println("onOptionSelected executed");
    switch (item.getItemId()) {
        case R.id.home:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message
            Toast.makeText(MainDeliveryBoyActivity.this, "Home is Selected", Toast.LENGTH_SHORT).show();
            // MenuHomeActivity
            startActivity(new Intent(context,MenuHomeActivity.class));
            return true;

        case R.id.delivered1:
            Toast.makeText(MainDeliveryBoyActivity.this, "delivered is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.cancelled:
            Toast.makeText(MainDeliveryBoyActivity.this, "cancelled is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.active:
            Toast.makeText(MainDeliveryBoyActivity.this, "active is Selected", Toast.LENGTH_SHORT).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

So my question is should I copy and paste all the above code in all the activities? or is there a way where I can skip this?

Kishore Kumar Korada
  • 1,204
  • 6
  • 22
  • 47
  • 1
    Create a BaseActvity class and override its method and extend that activity in all your other activity – Vaisakh N Oct 15 '15 at 04:35

2 Answers2

3

Create one global activity called BaseActivity and make all of your activities extend it.

public class BaseActivity extends AppCompatActivity{
    public void onCreate(Bundle iCreate){
        ...
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.del_boy_menu, menu);
        ....
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        ....
    }
}

And now all other activities should extend the BaseActivity, so you won't need to write code to inflate menu everytime.

public class Activity1 extends BaseActivity{
    ....
}
Apurva
  • 7,871
  • 7
  • 40
  • 59
1

I believe each activity to have a unique menu. But there is a way you can do what you are trying to implement here.

  • You can create a base class that inherits from an Activity class and put all your menu logic on that base class.

And you can also refer to this answer, Reuse the Action Bar in all the activities of app and this article.

PS: I do not take credit for the answer, I just want to help. Cheers!

Community
  • 1
  • 1
arvicxyz
  • 381
  • 3
  • 13