0

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pedro Igor
  • 19
  • 8

6 Answers6

1

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.

Intent Docs

SharePreferences

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I successfully used the SharedPreferences but to jump to the next activity, it was not shown in textview, only to entar in another activity and return to the previous which was visible :/ – Pedro Igor Feb 13 '14 at 19:29
  • Look at the `SharedPreferences` example I linked to. You just have to open up the prefs and retrieve it from there and you can use it in your `TextView` – codeMagic Feb 13 '14 at 19:31
  • could give me an example? – Pedro Igor Feb 13 '14 at 19:51
  • I already gave you one but [here are some more good examples](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – codeMagic Feb 13 '14 at 19:54
1

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!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
  • I successfully used the SharedPreferences but to jump to the next activity, it was not shown in textview, only to entar in another activity and return to the previous which was visible :/ – Pedro Igor Feb 13 '14 at 19:26
  • There's "Private to the Activity" Preferences and "Global Default" shared preferences, make sure you are using the one with the whole app scope, the post provided explains that issue in detail... – Martin Cazares Feb 13 '14 at 19:28
1

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);

Shared Preferences

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:

  • SQLite Databases
  • a Singleton class (But not always recommended if need persistent data)
  • and so on

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.

blackfizz
  • 822
  • 8
  • 21
0

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.

Vic Vuci
  • 6,993
  • 6
  • 55
  • 90
  • what would be the "context"? how to show the result in a textview now? – Pedro Igor Feb 13 '14 at 19:32
  • Context could be any context. Try using "this"; For the rest, look up some tutorials because you're asking very basic questions that have been answered many times before. https://developer.android.com/training/basics/firstapp/building-ui.html – Vic Vuci Feb 13 '14 at 23:19
0

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!

Pedro Igor
  • 19
  • 8
0

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");
                }
            }
        }
    }
a.black13
  • 2,157
  • 4
  • 19
  • 20