0

I'm new to android. I am taking a string and a numerical value as input from the MainActivity. On pressing the button, the following method is called which invokes the second activity. Am I passing the values correctly? If yes, how do I receive both these values for use in the second activity and then print them ?

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText1 = (EditText) findViewById(R.id.name);
    EditText editText2 = (EditText) findViewById(R.id.ma);
    String message = "Hi ! " + editText1.getText().toString();
    int i = Integer.parseInt(editText2.getText().toString());
    intent.putExtra("lol",message);
    startActivity(intent);
}

P.S. I know Im not passing the integer at all. I dont know how to do that. Please help!

saruftw
  • 1,104
  • 2
  • 14
  • 37

3 Answers3

0

You just use this one for integer values In second activity....

    int i = getIntent().getIntExtra(SendingStringName, 0);
    (or)
    String progress = getIntent().getStringExtra(SendingStringName);
    (or)
    ArrayList< String> progress = getIntent().getStringArrayListExtra(name);
Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12
  • Why do you need to give examples for `String` or `ArrayList` extras for this. Just the `getIntExtra` example should suffice. – AndyFaizan Mar 29 '14 at 14:15
  • Isn't getIntent() supposed to be used just once and then we extract all data? Can you explain using it this way? – saruftw Mar 29 '14 at 14:16
  • You can use these types data extraction I just post the these ways you can send the data And i posted the some example methods to extract other data it's extra information dude you may use this in future so only i put this...You just take the first line for your solution – Naveen Kumar Kuppan Mar 29 '14 at 14:24
0

From the new Activity you should call getIntent(): https://developer.android.com/reference/android/app/Activity.html#getIntent()

Once you have the Intent you have to call getIntExtra(String, int): https://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String,%20int).

In your case, from the new Activity something like:

Intent intent = getIntent();
int lol = intent.getIntExtra("lol", 0);

Or shorter:

int lol = getIntent().getIntExtra("lol", 0);

That '0' is the default value for the int if the extra "lol" does not exist.

ArianJM
  • 676
  • 12
  • 25
  • 1
    Thanks! Solvedit right away. But I still didnt understand the use of '0'. Why is it required when we already have the extra "lol" defined . – saruftw Mar 29 '14 at 14:24
  • If for some reason that extra didn't exist, then lol=0. That way it has a default value, and it's not just non existent. It is useless if you are sure to always have the extra "lol", but only if you are sure of that. – ArianJM Mar 29 '14 at 14:46
0

To send the data:

EditText editText1 = (EditText) findViewById(R.id.name);
EditText editText2 = (EditText) findViewById(R.id.ma);
String message = "Hi ! " + editText1.getText().toString();
int i = Integer.parseInt(editText2.getText().toString());

Bundle param = new Bundle();
param.putString("greeting",message);
param.putInteger("NumberInteger",i);

Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtras(param);
startActivity(intent);

To receive them: In Class DisplayMessageActivity.java do:

Intent it = getIntent();
Bundle param = it.getExtras();
String capturedMessage= param.getString("greeting");
Integer captured_I = param.getInteger("NumberInteger");