0

I want to navigate to another Activity when click on the option menu item 'settings' from the menu bar. Nothing actual happens.I have checked similar issues posted here, but i can understand why this is not working for option menu. see the code below:

Can't go to a new activity from selected option from option menu

<item
    android:id="@+id/mySettings"

    android:title="@string/action_settings" />

<item
    android:id="@+id/logout"

    android:title="log out" />

code:

public class Dashboard extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.app_bar_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }


    public void openConfigure(){
        Intent intent = new Intent(this,Configure.class);
        this.startActivity(intent);
    }
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.mySettings:
                openConfigure();

                break;

            default:
                return super.onOptionsItemSelected(item);

        }


        return true;
    }
coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
Pa Ebou
  • 3
  • 4

2 Answers2

1

Use onOptionsItemSelected instead of onContextItemSelected cause you are using OptionMenu not ContextMenu.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.mySettings:
            openConfigure();
            break;
    }
    return super.onOptionsItemSelected(item);
}
ADM
  • 20,406
  • 11
  • 52
  • 83
  • i have change it to onOptionsItemSelected , but still not opening the other Activity. – Pa Ebou Jun 10 '19 at 13:13
  • What happen when you click on menu item ? Also Check Manifest entry for `Configure` Activity it its not there ? – ADM Jun 10 '19 at 13:15
  • when i click no error shows, and is not opening the Configure Activity. the Manifest entry is also there: – Pa Ebou Jun 10 '19 at 13:24
  • your solutions works. however, it opens a different Activity. – Pa Ebou Jun 11 '19 at 08:05
  • It will open the Activity you stated inside `Intent` in this case `Configure` .. You probably messed up with code or Layout . – ADM Jun 11 '19 at 08:23
0

to select an options menu item, you have to override onOptionItemSelected() : try below code

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.mySettings:
            openConfigure();
            break;
    }
    return true;
}
Mitesh Machhoya
  • 404
  • 2
  • 8