0

I set-up a list of items for the MainActivity. Today only one item this menu.

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="fr.vincelec.sunshine.sunshine.app.ForecastFragment">

<item android:id="@+id/action_refresh"
android:title="@string/refresh"
app:showAsAction="never"/>

</menu>

Also Here is the code to enable the menu:

 @Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    inflater.inflate(R.menu.forecastfragment, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();
    if (id == R.id.action_refresh) {
        FetchWeatherTask weatherTask = new FetchWeatherTask();
        weatherTask.execute();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

But when I launch the program in my Android phone, and click on the menu, I see twice the item "Refresh", not only one item ...

I checked if there is something in MainActivity.java or main.xml but without success, It's only on my fragment file "ForecastFragment.java" (where the piece of code shown before is from) that I call the R.menu.forecastfragment .

1 Answer 1

0

Clear the menu items before you inflate it.

  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
        menu.clear();
        inflater.inflate(R.menu.forecastfragment, menu);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.