0

public class MainActivity extends Activity {

private Menu addMenuButton;  
@Override
    protected void onCreate(Bundle savedInstanceState)
        {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainact);
    public void onPopup(View view)
{
    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);


    toast.show();
    return true;
        }
    });
    menu.show();

} }

please tell the solution to move into another activity when click on the item list. I am new to program. This is my first program. Try to explain in depth. Thanks in advance.

4 Answers4

1

Intents are used to move from one activity to another.

An Intent is basically a message to say you did or want something to happen. Depending on the intent, apps or the OS might be listening for it and will react accordingly.

Explicit intent

Intent intent2 = new Intent(MainActivity.this, YourSpotActivity.class);
startActivity(intent2);

Remember to add your activity in your AndroidManifest

 <activity android:name="com.example.abc.YourActivity" > </activity>

This will call YourSpotActivity i.e this activity will be visible and MainActivity will remain on stack and when you press back button MainActivity will be visible again

  • In an Explicit intent, you specify the activity that is required to respond to the intent. In other words, you explicitly designate the target component.

  • In an Implicit intent , you just declare an intent and leave it to the android platform to find an activity that can respond to the intent.

Related Links:

What is an Intent in Android?

What is the format for an android intent?

Intent Tutorial

P.S- why have you commented the intent code ?? Uncomment it and your code will work

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • did you uncommented this part //Intent intent2 = new Intent(MainActivity.this, YourSpotActivity.class); //startActivity(intent2); First you accepted the answer than you unaccepted it and then you accepted some other answer !! I think you should learn to run your code successfully using a answer and then only accept it . – Rachita Nanda Jul 18 '13 at 06:14
  • ok ok i accept ur explanation.. i am new to this site.. so i was click both of ur answers correct.. now only i know it was choose only one.. – Rameshbabu Jul 18 '13 at 06:19
0

use the below code in onMenuItemClick block

startActivity(new Intent(Current_class.this,Destination.class));

it start navigation to destination class.

  • i am working in map program.. while click the list item named map, is was display toast message only. not go to map activity.. – Rameshbabu Jul 18 '13 at 05:52
0

Your commented code should work if you enable it. Is it not working?

//Intent intent2 = new Intent(MainActivity.this, YourSpotActivity.class);
//startActivity(intent2);

You need to add this new activity YourSpotActivity in your AndroidManifest.xml file for it to work.

Rajeev
  • 1,374
  • 1
  • 11
  • 18
0

Uncomment your code and add yourspotactivity in your Android Manifest File , this will solve your issue .