Reputation: 19
What's the best way to get get a string value in an activity A and save it activity B?
I think that there may be a relationship between Intents and SharedPreferences!
Someone?
Upvotes: 0
Views: 1721
Reputation: 2157
Simply way for store String value and restore in another activity is: 1)In first activity start activity that most return result as string (before create final int variable as like RETURN_DATA_AS_STRING)
Intent intent = new Intent(getApplicationContext(), ReturningResultActivity.class);
startActivityintent(intent,RETURN_DATA_AS_STRING);
2) In next activity for store data make next step:
Intent intent = new Intent();
intent.putExtra("SomeStringIdentifier", "this is resulting string");
setResult(RESULT_OK,intent);
3) In first activity override onActivityResult():
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case RETURN_DATA_AS_STRING: {
String thisIsResultStringFromAnotherActivity = data.getStringExtra("SomeStringIdentifier");
}
}
}
}
Upvotes: 0
Reputation: 19
Many thanks to all who helped me, I am really glad to see that there are important people who still care about us. The simple solution was a conflict between the ids of save buttons because possessed the same id in all activitys.Apenas this! My thanks to all!
Upvotes: 0
Reputation: 822
You can store different values in your intents. Here is the official Google Example: Google
Intent
In activity A:
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Intent intent = new Intent(this, ActivityB.class);
String message = "Your String";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
And in activity B:
Intent intent = getIntent();
String message = intent.getStringExtra(ActivityA.EXTRA_MESSAGE);
In activity A:
private static final String PREV_STRING_VALUE = "prevStringValue";
String value = "yourString";
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.edit().putString(PREV_STRING_VALUE, value).apply();
And in activity B:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String value = sp.getString(ActivityA.PREV_STRING_VALUE, "DefaultValue");
Or some other approaches:
There are so many ways to pass around your data. But which type of implementation you will need always depends on the situation. If you want your data only passed to the next activity/fragment and it's temporary data, then just use intents.
If you need your data in the further process of your app, then use SQLite databases or SharedPreferences (If it's not complex data). Save your value and you can access it whenever you want wherever you want.
Upvotes: 1
Reputation: 7051
You can do this a ton of ways..
One being with Preferences
so for example
int myVal = 16;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putInt("MY_KEY",myVal).commit();
Then, in your next activity
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.getInt("MY_KEY", 0); //Where 0 is the default value
You can also use intents, as you said
int myVal = 16
Intent intent = new Intent(); //Make your intent as you normally would
intent.putExtra("MY_KEY", myVal);
And in the onCreate()
of your next Activity:
int myVal = getIntent().getExtras().getInt("MY_VALUE");
However these are basic implementations. As Artoo Detoo said, there are many different ways of doing this. You can do this with fragments, static classes, other bundles, etc etc. It depends on the situation -- it would be best to use the intent way so you aren't just storing values in preferences for no reason. If you only need it to go from activity A to activity B, just use the intent method. If you need it in many different places but don't wish to keep track of it? Use prefs.
Upvotes: 0
Reputation: 13705
There's different ways to approach this issue, but the one that you choose actually depends on your needs, this post explains the different ways you can take to get that.
http://www.doepiccoding.com/blog/?p=153
Basically, there is:
-Shared Preferences
-Intent Extras
-Application Subclass
-SQLite Database
All of them are explained in detail...
Regards!
Upvotes: 1
Reputation: 44571
What's the best way to get get a string value in an activity A and save it activity B?
This depends on what you mean by "save it activity B"
If you just want to "pass" it to B
and use it there then just send in an Intent
Intent i = new Intent(..., ...);
i.putExtra("someKey", someString);
startActivity(i);
and get it in B
(not before onCreate()
) with something like
Intent intent = getIntent();
String foo = intent.getStringExtra("someKey");
If you want it to actually persist then SharedPreferences
would be good. There is a good example in the docs.
Upvotes: 1