0

I am calling an Activity using an Intent, and I need to pass variables to that Activity when is initialized. On iOS you can use a custom initialization using the initWithNibName method. How can a similar thing be achieved on Android?

Here is my code that creates an Intent...

Intent myIntent = new Intent(webPush.this, webPushActivity.class);
startActivity(myIntent);
wattostudios
  • 8,446
  • 13
  • 40
  • 54
Jaume
  • 3,318
  • 16
  • 50
  • 108

4 Answers4

5
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
myIntent.putExtra("mystring",strValue)' <<---put String here 
startActivity(myIntent);

and in second Activity...

String str = getIntent.getExtras().getString("mystring");<<get string in second class

and check this

How do I pass data between Activities in Android application?

Community
  • 1
  • 1
Samir Mangroliya
  • 38,074
  • 16
  • 111
  • 131
2

You can put extra data into the Intent...

myIntent.putExtra("exampleString","This is some extra data");
myIntent.putExtra("exampleNumber",1234);

When you call the Intent, it starts the Activity. in one of the main methods of the Activity, like onCreate(), you can access the Intent and get the extras from it, like so...

Intent callingIntent = getIntent();
String exampleString = callingIntent.getStringExtra("exampleString");
int exampleNumber = callingIntent.getIntExtra("exampleNumber");
wattostudios
  • 8,446
  • 13
  • 40
  • 54
1

You can set extras to the intent:

myIntent.putStringExtra("First key", 1);
myIntent.putStringExtra("Second key", "some string");

And then get it in the new activity

Int extraInt = getIntent().getIntExtra("First key");    
String extraString = getIntent().getStringExtra("Second key");

See more in the Intent docs

MByD
  • 129,681
  • 25
  • 254
  • 263
1

This can be done with intent extras. For example:

int variable = 6;
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
myIntent.PutExtra("stringLabel", variable);
startActivity(myIntent);
joates
  • 1,083
  • 1
  • 11
  • 20