0

Possible Duplicate:
How do I pass data between activities in Android?

I have three activities in my application and each activity is depending on another. I'm currently using static variables to pass objects and values between these activities. Problem with this is that it get's very confusing fast and it's hard to keep track of when I assigned that global variable a value etc. I'm thinking of implementing an interface between these activities to make the code clearer and easier to understand. Thing is, i'm not entirely sure this is the best way to go so any help or advice would be great.

Community
  • 1
  • 1
user1163392
  • 167
  • 1
  • 1
  • 8

3 Answers3

1

use putExtra to send info to another activity
send:

Bundle bundle = new Bundle();
bundle.putString(“name″, “username”);

Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);

Receive:

Bundle bundle = this.getIntent().getExtras();
String data = bundle.getString(“name″);

data = username

Andreas
  • 2,007
  • 5
  • 26
  • 37
  • I don't see how a bundle would make it any clearer then a global variable does. – user1163392 Mar 13 '12 at 10:30
  • Bundles are the default way to pass data between activities in android. Your onCreate (the 'main' of your activity) always has a bundle parameter). See also: http://stackoverflow.com/a/6480551/706130. – Andreas Mar 13 '12 at 10:35
0

I believe what you want is the Intent.putExtra() method. There are several methods depending on what kind of data you want to pass. See the documenation here.

Krøllebølle
  • 2,878
  • 6
  • 54
  • 79
0

to pass data:-

Intent i = new Intent();
 i.putExtra("key", "data");
 startActivity(i);