K
K
kolodach2014-02-14 08:12:32
Java
kolodach, 2014-02-14 08:12:32

How to call a new activity when clicking on an ActionBar menu item?

I am learning to write under android, there was a need to call a new activity when I click on the ActionBar menu item, and if I register it in the onCreateOptionsMenu method as a regular button, the application falls, how can I write it correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bimeg, 2014-02-14
@kolodach

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item
        android:id="@+id/menu_item"
        android:icon="@drawable/item_icon"
        android:showAsAction="ifRoom"
        android:title="Menu Item"/>
    
</menu>

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

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
  switch (item.getItemId())
  {
    case R.id.menu_item:

      Intent intent = new Intent(this, NextActivity.class);
      startActivity(intent);

      return true;
  }

  return super.onOptionsItemSelected(item);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question