How to start different activity using switch case in the menu item?

android, android-activity, listener, switch-statement

Solution

You need to use switch as below

  switch (item.getItemId()) {
    case R.id.menuitem1:
    Toast.makeText(getApplicationContext(), "StartActiviy 1", Toast.LENGTH_SHORT).show();
        // start activity 1
       return true;
    case R.id.menuitem2:
    Toast.makeText(getApplicationContext(), "StartActiviy 2", Toast.LENGTH_SHORT).show();
       // start activity 2
       return true;
     default:
      //default intent
       return true;
     }

http://developer.android.com/reference/android/widget/PopupMenu.html

Problem

``` public void onPopup(View view) { final PopupMenu menu=new PopupMenu(this,view); menu.getMenuInflater().inflate(R.menu.menu1,menu.getMenu()); menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { Toast toast=Toast.makeText(MainActivity.this, item.getTitle()+"Selected",Toast.LENGTH_SHORT); //Intent intent2 = new Intent(MainActivity.this, YourSpotActivity.class); //startActivity(intent2); //startActivity(new Intent(MainActivity.this,YourSpotActivity.class)); toast.show(); return true; } }); menu.show(); } ``` When i click any one of the list item then it will start another activity. How can i do that by modify an above code. explain me please. I have use four car model in the menu. when i choose any one of that car then it will go to particular activity.

Original source

Related problems