How to insert onCreateOptionsMenu into a Java class?

Asked

Viewed 202 times

0

How to insert the onCreateOptionsMenu and the onOptionsItemSelected within a Java class not to be repeating in all Activity calling it ?

I have to pass only the ids and classes I call when clicking on the menu item.

@Override
public boolean onCreateOptionsMenu(Menu menu){

MenuInflater MenuItem = getMenuInflater();
// Aqui deve-se passar o xml como parametro da função para a outra classe
MenuItem.inflate(R.menu.novo_menu,menu); 

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {


switch(item.getItemId()){
    case R.id.id_c:

         // Aqui deve-se passar as 2 classes pela função para colocar no Itent
         Intent tela = new Intent(Tela1.this, tela2.class);
         startActivity(tela);
         break;

    case R.id.id_s:
        // Aqui deve-se passar as 2 classes pela função para colocar no Itent
        Intent tela = new Intent(Tela1.this, tela3.class);
        startActivity(tela);
        break;
}


return super.onOptionsItemSelected(item);
}
  • I believe that what you really should do is to have a parent Activity and load your layout in Fragments, it would be simpler, no?

  • Or if you want to keep the activitys and put for example, different functions for each item in different activitys, you can put an interface that calls a method in your Activity when for example an item is chosen, in it you pass as parameter the id to make the comparison

  • But I still find the most appropriate Ragments solution for this case

  • @Woton Sampaio, can you give an example of how to do this using Fragments ? I didn’t know you could do this.

1 answer

1

As I said, if you use Fragments, they will all have the parent Activity, in it you implement the 2 methods, if case for each fragment you want them to options have a different function, you can do so:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(tal_fragment.isVisible()){
        switch (id) {
            case R.id.item1:
              //Seu código
            default:
                return super.onOptionsItemSelected(item);
    }else if(outro_fragment.isVisible()){
            switch (id) {
                case R.id.item1:
                    //Seu código
                default:
                    return super.onOptionsItemSelected(item);
    }
}

See? Depending on which fragment is currently open, the items have different functions. I don’t know if that’s what you want, but give a feedback.

If what you want is just that your NavigationDrawer and your Menu are visible in all fragments, is the same thing, however I believe that its functions will be the same, these methods you implement in Activity father.

  • I think that’s not quite it, because I only have a menu that I call in several Activity, the difference is that when I click on the item it calls a different screen in Intent. But the items are the same. What I wanted was to play this in a class and instantiate this class in the Activity that has the menu not to repeat code.

  • Why create activitys? You should use Fragments in this case as I said

  • Why are several activitys, which has menu. The menu is only a part of it, but the activitys are totally different from each other only the menu that is equal. But I will give a more in-depth research on this question of Fragments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.