Каков правильный синтаксис для вызова onCreateOptionsMenu() и onPrepareOptionsMenu() в Android Studio? ⇐ Android
Каков правильный синтаксис для вызова onCreateOptionsMenu() и onPrepareOptionsMenu() в Android Studio?
In Android Studio, I came across different ways to implement onCreateOptionsMenu() and onPrepareOptionsMenu() each with respect to the return statement in an Activity that extends from AppCompatActivity. It is quite confusing to me as to which one is the right way to implement the two methods, given that I want to maintain equality of syntax all across my application.
onCreateOptionsMenu() Example 1: Only true is returned.
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } Example 2: Call to super class is returned. @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu_activity_dashboard, menu); return super.onCreateOptionsMenu(menu); } Example 3: Call to super class is done in the first line and true is returned later. @Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions( R.id.intent_group, 0, 0, this.getComponentName(), null, intent, 0, null); return true; } As you see, in the third example, call to super class is done followed by returning true. But in the first two examples, either true or call to super class is returned. Which one is the right way to do it?
onPrepareOptionsMenu() Example 1: Call to super class is returned.
@Override public boolean onPrepareOptionsMenu(Menu menu) { if(Build.VERSION.SDK_INT > 11) { invalidateOptionsMenu(); menu.findItem(R.id.option2).setVisible(false); menu.findItem(R.id.option4).setVisible(true); } return super.onPrepareOptionsMenu(menu); } Example 2: Call to super class is done in the first line and true is returned later. @Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_save).setVisible(true); menu.findItem(R.id.action_reset).setVisible(true); menu.findItem(R.id.action_about).setVisible(true); return true; } Here too the same question arises. Which syntax should I follow to implement the method properly?
Источник: https://stackoverflow.com/questions/780 ... optionsmen
In Android Studio, I came across different ways to implement onCreateOptionsMenu() and onPrepareOptionsMenu() each with respect to the return statement in an Activity that extends from AppCompatActivity. It is quite confusing to me as to which one is the right way to implement the two methods, given that I want to maintain equality of syntax all across my application.
onCreateOptionsMenu() Example 1: Only true is returned.
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } Example 2: Call to super class is returned. @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu_activity_dashboard, menu); return super.onCreateOptionsMenu(menu); } Example 3: Call to super class is done in the first line and true is returned later. @Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions( R.id.intent_group, 0, 0, this.getComponentName(), null, intent, 0, null); return true; } As you see, in the third example, call to super class is done followed by returning true. But in the first two examples, either true or call to super class is returned. Which one is the right way to do it?
onPrepareOptionsMenu() Example 1: Call to super class is returned.
@Override public boolean onPrepareOptionsMenu(Menu menu) { if(Build.VERSION.SDK_INT > 11) { invalidateOptionsMenu(); menu.findItem(R.id.option2).setVisible(false); menu.findItem(R.id.option4).setVisible(true); } return super.onPrepareOptionsMenu(menu); } Example 2: Call to super class is done in the first line and true is returned later. @Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_save).setVisible(true); menu.findItem(R.id.action_reset).setVisible(true); menu.findItem(R.id.action_about).setVisible(true); return true; } Here too the same question arises. Which syntax should I follow to implement the method properly?
Источник: https://stackoverflow.com/questions/780 ... optionsmen
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Каков правильный синтаксис использования «docker Compose» при написании сценария?
Anonymous » » в форуме Linux - 0 Ответы
- 38 Просмотры
-
Последнее сообщение Anonymous
-