Kishore Kumar Korada
Kishore Kumar Korada

Reputation: 1264

Displaying menu in all the activities without overriding onCreateOptionsMenu and onOptionsItemSelected each time

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?

Upvotes: 1

Views: 919

Answers (2)

dovicz
dovicz

Reputation: 380

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!

Upvotes: 1

Apurva
Apurva

Reputation: 7901

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{
    ....
}

Upvotes: 3

Related Questions