Same option menu in all Activities in Android

后端 未结 3 706
面向向阳花
面向向阳花 2020-12-23 12:01

I have 10-15 activities in my project. I want to have the option menu mostly in all Activities. Then is their any way we can do it at one place and it appears in all activit

相关标签:
3条回答
  • 2020-12-23 12:25

    Create a Class (say BaseActivity) that extends Activity, and override onCreateOptionsMenu and onOptionsItemSelected functions.

    public class BaseActivity extends Activity {
    
        // Activity code here
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.options_menu, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.item:
                    // do what you want here
                    return true;
                default:
                   return super.onOptionsItemSelected(item);
            }
        }
    }
    

    Now, in the other 15-16 activities, instead of extending an Activity, you should extend BaseActivity.

    public class FooActivity extends BaseActivity { 
    
        // Activity code here
    
    }
    

    This way, all your activities derive the options menu. For activities where you want the options menu disabled, you can override it again in that particular activity.

    public class BarActivity extends BaseActivity { 
    
        // Activity code here
    
       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
           // Do Nothing
       }
    
       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
           // Do Nothing
       }
    }
    

    Hopefully, it doesn't give you problems in the manifest file.

    0 讨论(0)
  • 2020-12-23 12:26

    The solution to this problem is in your new activity add this menu method.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_home_page, menu);
        menu.removeItem(R.id.logout);
        return true;
    }
    

    In each activity this method will automatically appear.

    If it doesn't then add it with the inflate call. It requires two parameters, an xml resource(the same one that you used in your original activity), and the menu object that is pass into the onCreateOptionsMenu method.

    menu.removeItem will remove the menu item of whatever resource id you pass to it. I hope this helps those who are facing this problem.

    Thank you, and happy to share this post.

    0 讨论(0)
  • 2020-12-23 12:27

    It is not enough to just extend the BaseActivity, you must also call super.onCreateOptionsMenu(menu) and super.onOptionsItemSelected(item) like this in your other activities:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        //getMenuInflater().inflate(R.menu.menu_second, menu);  <- remove this
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
提交回复
热议问题