0

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?

4
  • Why do you need something like that? You app will have only one active activity Commented Apr 10, 2013 at 16:16
  • What's the sense of passing data to an Activity which doesn't even exist at the moment? Commented Apr 10, 2013 at 16:17
  • there is a way to do this ? its a little bit hard to explain... Commented Apr 10, 2013 at 16:18
  • could you explain what are you trying to achieve? as you have been told, you cannot have two activities open at a time, maybe you should take a look to fragments : developer.android.com/guide/components/fragments.html Commented Apr 10, 2013 at 16:21

2 Answers 2

2

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); 
Sign up to request clarification or add additional context in comments.

6 Comments

i actually need somthing to send data to other class while its closed now.
@yair If your activity is paused(closed) you can't send anything to it . The only thing you can do is let the activity get that data when it resumes. Check out the onResume() method of an activity and SharedPreferences as others have suggested.
With the SharedPreference you will save your data, and you can use it in anywhere, works like a small DB
with SharedPreference i can only store the data no? how i can also pass it to the next Activity ?
@yair you can get the data you stored in SharedPreferences in any activity whenever you want as I said it before, you can get the data stored on your onResume() method of the activity you want to pass the data
|
0

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.

2 Comments

im just trying to send data to another class that in this time is closed. but the user always can go back to the menu and access this class , so when he want to go back and access it he will have the data there already.
In this case,try serializing to a SharedPreferences.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.