1

am trying to send a variable from one activity to another i have set up an intent to send to the second activity. what i want to know is in the second activity what do i need to do in order to be able to use that variable in an if statement?

heres my code

Intent mainIntent = new Intent(TheLeagueActivity.this,IntroActivity.class);

                                    mainIntent.putExtra("leagueCount", leagueCount);

                                    TheLeagueActivity.this.startActivity(mainIntent);

                                    TheLeagueActivity.this.finish();
iamlukeyb
  • 6,487
  • 12
  • 29
  • 40

4 Answers4

1

String strExtra = getIntent().getExtras().getString("leagueCount");

...that's it! ;) (Depending on what dataType u put in, u have to use "getInt()" or sth..)

Thkru
  • 4,218
  • 2
  • 18
  • 37
0

In the target activty, call getIntent to retrieve the intent, then use getStringExtra, getIntExtra, etc. to retrieve the intent parameters.

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
0

Depends what is your variable. It seems to be an int. So you will call like this:

int myVar = getIntent().getExtras().getInt("leagueCount");

if (myVar == 2) {
    //do the stuff
}
Dany's
  • 943
  • 1
  • 7
  • 17
0

in onCreate() method of IntroActivity write the following code

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Intent intent = getIntent();

    // -1 is default value if no value associated with key "leagueCount"

    int leagueCount = intent.getIntExtra("leagueCount", -1);

   /*
      if leagueCount is not equal to -1
      use leagueCount here

   */
}
Ravi1187342
  • 1,247
  • 7
  • 14