Search code examples
androidandroid-layoutandroid-fragmentsandroid-studiomenuitem

When I open new layouts, the menu item duplicates


I'm learning how to create an app and have figured out how to build a login activity and a menu for logging out that returns you to the login layout. However, if I navigate through my different layouts/activities, the menu item for logging out duplicates. I believe it's due to having the menu created on each layout, but I'm not sure how to change it so that it doesn't duplicate.

Here's my fragment.

    public class UserFragment extends Fragment {

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
        savedInstanceState) {
    return inflater.inflate(R.layout.activity_user_fragment, container, false);
}

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

My LoginActivity.class

public class LoginActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);

    Button b1login = (Button) findViewById(R.id.btlogin);
    Button b2login_cancel = (Button) findViewById(R.id.btcancel_login);

    assert b1login != null;
    b1login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText ed1 = (EditText) findViewById(R.id.etuser_name);
            EditText ed2 = (EditText) findViewById(R.id.etpassword);
            if (ed1.getText().toString().equals(getText(R.string.user_id)) &&
                    ed2.getText().toString().equals(getText(R.string.user_password))) {
                Toast.makeText(getApplicationContext(), R.string.successful_login,
                        Toast.LENGTH_SHORT).show();
                setContentView(R.layout.activity_clients);


            } else {
                Toast.makeText(getApplicationContext(), R.string.unsuccessful_login,
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

    assert b2login_cancel != null;
    b2login_cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.exit(0);
        }
    });
}

//Menu option logout return to login screen.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.menu_item_logout) {
        Intent i = new Intent(this, LoginActivity.class);
        this.startActivity(i);
        return true;
    }
    return super.onOptionsItemSelected(item);
}



public void addClient(View view) {
    setContentView(R.layout.activity_new_client);
}

public void submitClient(View view) {
    setContentView(R.layout.activity_sessions);
}

public void cancelClient(View view) {
    setContentView(R.layout.activity_clients);
}


public void newSession(View view) {
    setContentView(R.layout.activity_new_session);
}

public void cancelSessionCompletion(View view) {
    setContentView(R.layout.activity_sessions);
}

public void cancelSession(View view) {
    setContentView(R.layout.activity_sessions);
}
}

Fragment layout.

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/menu_item_logout"
    android:icon="@drawable/ic_logout"
    android:title="@string/logout"
    app:showAsAction="ifRoom|withText" />
</menu

EDIT:

I deleted the fragment where I initially created the menu as well as the menu code from the UserFragment and hard coded the menu itself into the LoginActivity class, which fixed the duplication issue.

Code adding menu.

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
    }

However, the menu now shows on the login screen. How do I prevent it from showing there?


Solution

  • For having different menu items in different activities you need to create different menu xml layouts.

    So one file named menu.xml is already there in res folder. You need to create new xml file in same folder.

    Then in java code of that activity :- in override Oncreateoptionmenu inflate the menu you want.