I have a TextView in Activity A and that is where the user spends most of the time on the app. My app uses Shared Preferences to save a TextView in Activity C. How do I get the TextView from Activity C without going to activity C when I load Activity A and/or B. I know I can get the TextView from Activity C with intent but I think that only works if I'm coming from Activity C which I am not.
Activity A currently get's the TextView this way
Intent id = getIntent();
if (id.getCharSequenceExtra("idid") != null) {
final TextView setmsg = (TextView)findViewById(R.id.loginid2);
setmsg.setText(id.getCharSequenceExtra("idid"));
}
But this only works if another Activity used putExtra to get it there.
You don't get the TextView but a String preference:
In your activity's layout you put a TextView and setText with the preference value
You can't save a
View
instance withSharedpreferences
. Only primitive types can be saved inSharedPreferences
. See theSharedPreferences.Editor's
put methods.To pass your
TextView's
String
value from oneActivity
to another, eithereditor.putString(...)
and retrieve it in Activity B, orIntent
.From the question, what I understood is,
Whenever I load Activity A / B , its Text will show the value from Activity C. Isn't it?
Of course we can use SharedPreference. try this:
inside Activity C ->
And now, inside Activity A onCreate ->
Thats it.