How do I pass data between Activities in Android application?

ID : 792

viewed : 174

Tags : androidandroid-intentandroid-activityandroid

Top 5 Answer for How do I pass data between Activities in Android application?

vote vote


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.

vote vote


The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class); intent.putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent); 

Access that intent on next activity:

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID"); 

The docs for Intents has more information (look at the section titled "Extras").

vote vote


Passing Intent extras is a good approach as Erich noted.

The Application object is another way though, and it is sometimes easier when dealing with the same state across multiple activities (as opposed to having to get/put it everywhere), or objects more complex than primitives and Strings.

You can extend Application, and then set/get whatever you want there and access it from any Activity (in the same application) with getApplication().

Also keep in mind that other approaches you might see, like statics, can be problematic because they can lead to memory leaks. Application helps solve this too.

vote vote


Source class:

Intent myIntent = new Intent(this, NewActivity.class); myIntent.putExtra("firstName", "Your First Name Here"); myIntent.putExtra("lastName", "Your Last Name Here"); startActivity(myIntent) 

Destination Class (NewActivity class):

protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.view);      Intent intent = getIntent();      String fName = intent.getStringExtra("firstName");     String lName = intent.getStringExtra("lastName"); } 
vote vote


You just have to send extras while calling your intent.

Like this:

Intent intent = new Intent(getApplicationContext(), SecondActivity.class); intent.putExtra("Variable name", "Value you want to pass"); startActivity(intent); 

Now on the OnCreate method of your SecondActivity you can fetch the extras like this.

If the value you sent was in long:

long value = getIntent().getLongExtra("Variable name which you sent as an extra", defaultValue(you can give it anything)); 

If the value you sent was a String:

String value = getIntent().getStringExtra("Variable name which you sent as an extra"); 

If the value you sent was a Boolean:

Boolean value = getIntent().getBooleanExtra("Variable name which you sent as an extra", defaultValue); 

Top 3 video Explaining How do I pass data between Activities in Android application?