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?
– Woton Sampaio
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
– Woton Sampaio
But I still find the most appropriate Ragments solution for this case
– Woton Sampaio
@Woton Sampaio, can you give an example of how to do this using Fragments ? I didn’t know you could do this.
– Beto