How do I pass data between Activities in Android application?

UMAR-MOBITSOLUTIONS picture UMAR-MOBITSOLUTIONS · Jan 19, 2010 · Viewed 996.9k times · Source

I have a scenario where, after logging in through a login page, there will be a sign-out button on each activity.

On clicking sign-out, I will be passing the session id of the signed in user to sign-out. Can anyone guide me on how to keep session id available to all activities?

Any alternative to this case

Answer

user914425 picture user914425 · Sep 6, 2011

In your current Activity, create a new Intent:

String value="Hello world";
Intent i = new Intent(CurrentActivity.this, NewActivity.class);    
i.putExtra("key",value);
startActivity(i);

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("key");
    //The key argument here must match that used in the other activity
}

Use this technique to pass variables from one Activity to the other.