Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Manage menu items visibility in fragments

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 740
    Comment on it

    Creating menus in activities is a good choice but what would be if we are creating menus in each fragments ?

    Basically a fragment depends on Activity life cycles and its parent activity features so if you want to create different different menu options in every fragment then first of all you have to create all these menu items in parent activity like this :

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.findItem(R.id.iconAdd).setVisible(false);
        menu.findItem(R.id.iconOpt).setVisible(false);
        menu.findItem(R.id.iconRefresh).setVisible(false);
        menu.findItem(R.id.iconDone).setVisible(false);
        menu.findItem(R.id.iconIntroRefresh).setVisible(false);
    
        super.onCreateOptionsMenu(menu, inflater);
    }
    

    and in your fragment if you want to show iconAdd , iconOpt in menu then you can do like this :

       @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.findItem(R.id.iconAdd).setVisible(true);
        menu.findItem(R.id.iconOpt).setVisible(true);
        menu.findItem(R.id.iconRefresh).setVisible(false);
        menu.findItem(R.id.iconDone).setVisible(false);
        menu.findItem(R.id.iconIntroRefresh).setVisible(false);
    
        super.onCreateOptionsMenu(menu, inflater);
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: