yair
yair

Reputation: 43

How to pass data between activities

My question is how to pass data like String between two activities. Normally I would do this:

Intent i = new Intent(thisclass.this,NextClass.class); 
Bundle b = new Bundle();
i.putExtras(b);
b.putString("Name",Name);
StartActivity(i);

But this would make my Activity close and will open the next Activity, no? Is there any way that I can only pass data without opening the other activity?

Upvotes: 0

Views: 135

Answers (2)

Guilherme Gregores
Guilherme Gregores

Reputation: 1048

I think you are looking for something with the SharedPreference see the documentation : SharedPreference

if it is what are looking for.

Try this:

Global:

private SharedPreferences pref;

onCreate:

pref = this.getSharedPreferences("SharedPreference", Context.MODE_PRIVATE);

The place where you gonna save your data:

String data = "yourData"
pref.edit().putString("myData", data).commit();

And the other Activity:

Global:

private SharedPreferences pref;

onCreate:

pref = this.getSharedPreferences("SharedPreference", Context.MODE_PRIVATE);

The place where you gonna take your data:

String dataFromFristActivity = pref.getString("myData", null);

Upvotes: 2

pbhowmick
pbhowmick

Reputation: 1113

Your calling activity does not close but is paused. I am with @Egor on this. The called activity does not yet exist. Unless u are trying to send data between activities in two different apps. That is a whole different can of worms.

Upvotes: 0

Related Questions