Michal_196 Michal_196 - 5 months ago 19
Android Question

Why items in Android menu gets add after each swipe?

I wanted to move some of my items in toolbar from right to left, for this I have used

android.support.v7.widget.ActionMenuView
, but I have ran into trouble.

My activity contains three fragments. Now for example if I open menu from toolbar at app startup, all is fine. But if I swipe to B from A, items in the menu gets double. On startup menu contains two items, but after swipe to another fragment, items get double(it becomes four and so on).

I have tried searching internet, but this type of question is not posted anywhere.

My layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="2dp"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>

<cn.nekocode.toolbarindicator.ToolbarIndicator
android:id="@+id/indicator"
android:layout_height="match_parent"
android:layout_width="120dp"
android:layout_gravity="center" />
<android.support.v7.widget.ActionMenuView
android:id="@+id/amvMenu"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"/>
</android.support.v7.widget.Toolbar>

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

</LinearLayout>


MainActivity:

public class MainActivity extends AppCompatActivity{

ViewPager viewPager;
TabsPagerAdapter adapter;
Toolbar toolbar;
private SharedPreferences savedPreferences;
ToolbarIndicator toolbarIndicator;
ActionMenuView amvMenu;

CharSequence Titles[] = {"one", "two", "three"};
int Numboftabs = 3;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {

savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//here is the problem
toolbar = (Toolbar) findViewById(R.id.tool_bar);
if (toolbar != null) {
toolbar.setTitle("");
toolbar.setSubtitle("");
setSupportActionBar(toolbar);

}
adapter = new TabsPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
// Assigning ViewPager View and setting the adapter
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(2);
// Assiging the fadingIndicator View
toolbarIndicator = (ToolbarIndicator) this.findViewById(R.id.indicator);
amvMenu = (ActionMenuView) toolbar.findViewById(R.id.amvMenu);
amvMenu.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return onOptionsItemSelected(menuItem);
}
});
toolbarIndicator.setViewPager(viewPager);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, amvMenu.getMenu());
return true;
}

//handling the tap on the menu's items
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings: {//open settings
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
case R.id.exit:{
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
default:
break;
}

return super.onOptionsItemSelected(item);
}


}


If I edit this line
getMenuInflater().inflate(R.menu.menu_main, amvMenu.getMenu());
to
getMenuInflater().inflate(R.menu.menu_main, menu);

all gets fine but item appears on right again.

Answer

According to the comment section: Clear the menu in onCreateOptionsMenu() before the inflate.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    amvMenu.getMenu().clear(); // add this line
    getMenuInflater().inflate(R.menu.menu_main, amvMenu.getMenu());
    return true;
}
Comments