I have seen there are two ways to do this:
Creating a variable then using that variable to call the .inflate
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_add, menu);
return true;
}
And just assigning directly to the method
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_add, menu);
return true;
}
Is there a right and a wrong way to do this or are they essentially the same?
Thanks
They’re essentially the same. The advantages to using the variable method, though, are debugging (if you want to look at the inflater itself) and performance if you’re going to use the inflater more than once. If you’re just using it once, as in your second example, it’s probably a little better performance wise to forgo the variable, though the difference in the big picture is negligible.