-2

I have many Activities and in MainActivity and there is two Button (B1-B2). In B1 (Activity1) will user write same data and after finish back to MainActivity and in B2 (Activity2) will get all date from (Activity1)

(Activity1)

 Intent i = new Intent(getApplicationContext(), Activity2.class);
    i.putExtra("new_variable_name","value");

(Activity2)

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");


}  

this code doesn't work with me !!....any solve!!

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
BuGsHaN
  • 1
  • 3
  • http://stackoverflow.com/questions/4967740/transfer-data-from-one-activity-to-another-activity-using-intents http://stackoverflow.com/questions/18146614/how-to-send-string-from-one-activity-to-another – Soatl Jun 27 '14 at 20:55

2 Answers2

0

From the SO Posts:

Intent i =new Intent(Info.this, GraphDiag.class).putExtra("new_variable_name", "value");
startActivity(i);

To get the data

String value  = getIntent().getStringExtra(<StringName>);

Also look here

Soatl
  • 10,224
  • 28
  • 95
  • 153
0

Your code is correct I guess the context getApplicationContext() is causing the problem. In activity scope it is always better to use MyActivity.this as the context parameter. Try this:

Intent i =new Intent(Activity1.this, Acivity2.class);
i.putExtra("new_variable_name", "value");
startActivity(i);

To get the extra value and perform a null check:

Bundle b = getIntent().getExtras();
if(b!=null){
String value = b.getString("new_variable_name");//ensured that the string is not null
}
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • but how will know it's null or not? – BuGsHaN Jun 28 '14 at 11:23
  • it's work with this lain startActivity(i); i don't need to startActivity ...if user back to MainActivity after than go to Acivity2 and will get all data in ListView or Toast?? – BuGsHaN Jun 28 '14 at 11:26
  • Bundle b = getIntent().getExtras(); when put it this ..not work with me – BuGsHaN Jun 28 '14 at 11:40
  • now it's work but i don't need to startActivity(i); i want go back and press B2 and then will get all data in ListView or Toast – BuGsHaN Jun 28 '14 at 12:09
  • @BuGsHaN sorry for this comment. Its really simple so just search yourself and find an answer. You keep on nagging about it work it not work its null its not null and blah blah blah. and your last comment above its not even a part of the question asked. I know you are new to SO but you need to know that we are here only to help and not to guide you throughout your problem. – Illegal Argument Jun 28 '14 at 12:41
  • 1
    thanks for your comment ..i will try for solve my problem and thanks try again – BuGsHaN Jul 01 '14 at 14:37