1

I want to start a new activity with a variable passed in it.

Code so far:

if(mostLikelyThingHeard.toUpperCase().equals("PLAY ONE"))
{
    startActivity(new Intent("com.shaz.new"));
    //I want to send an int value `1` to the new activity.
}

Can this be done?

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
userxxx
  • 726
  • 9
  • 18
  • 1
    this how: http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – oentoro Apr 24 '13 at 17:48
  • 3
    Honestly, this is very basic Android... I would suggest you start reading developer Training section (http://developer.android.com/training/index.html) – Joris Apr 24 '13 at 17:51

2 Answers2

7

use putExtra(key, value)

Do like this

 if(mostLikelyThingHeard.toUpperCase().equals("PLAY ONE"))
    {
    Intent i=new Intent("com.shaz.new");
     i.putExtra("key","value")
    startActivity(i);
    }

Retrieve:

int value =getIntent().getIntExtra("key", 0); 0 is default value

Pragnani
  • 19,755
  • 6
  • 46
  • 74
1

Suppose there have two class one A and another one is B. You want to pass some data from A to B.

FROM CLASS A :

Intent result = new Intent(A.this,B.class);

result.putExtra("videoId", videoId);

result.putExtra("title",titleEdit.getText().toString());

result.putExtra("des", descriptionEdit.getText().toString());

result.putExtra("gps", passingGPS);

startActivity(result);

By above code you have start B activity from A and have passed some data. When B activity will start you have to get those data which you have passed in the time of calling B class from A class. You have to follow like this way to get the value of A class which you have passed.

From Class B:

VideoID = getIntent().getExtras().getString("videoId");

GPS = getIntent().getExtras().getString("gps");

Description = getIntent().getExtras().getString("des");

Title = getIntent().getExtras().getString("title");

Now you will able to use those value in class B.

Hope this will help you to "Start a new activity with passing some data".

jsonknightlee
  • 45
  • 1
  • 9
Siddiq Abu Bakkar
  • 1,909
  • 1
  • 13
  • 23