Passing strings between activities in android
In your LocationActivity class:
Intent i = new Intent(this, FindAndroidActivity.class);
i.putExtra("KEY",YourData);
In FindAndroidActivity class
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value = extras.getString("KEY");
}
Couple of scenarios:
-
If you want to pass the string when you start the new activity, then add it to the starting Intent and retrieve it in the new activity's
onCreate
.
Sending arrays with Intent.putExtra// Sending activity String latLong = "test"; Intent i = new Intent(sendingClass.this, receivingClass.class); i.putExtra("latLong", latLong); startActivity(i); // Receiving activity Bundle extras = getIntent().getExtras(); String latLong = extras.getString("latLong");
If you want to pass the string when you return from an activity, then use
startActivityForResult
and implement theonActivityResult
event
http://micropilot.tistory.com/1577The 3rd scenario, passing the string between two activities running at the same time is not possible because only one activity can run (be in the foreground) at a time.