Reputation: 17
Hey, I would like to get a hint in the right path of how to update the TextView
in a Activity
.
The situation is a current Activity1
computes a given task and the result is supposed to be displayed in the Activity2
while the user clicks on it. And when the user goes back to Activity1
computes and then clicks, the result should be updated.
But instead the result comes out to be a previous value.
Upvotes: 0
Views: 87
Reputation: 3975
You can pass/get values by using bundles, sharedpreferences, or using static variables which are not effective because they are temporary.
Edit (You asked to maintain on back as well so giving example for shared preference):
This is your SharedPreferences class, which is static so you can reach from every activity.
public class SharedPref {
public static void setDefaults(String key, String value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
}
This is how you are going to set and get your data in/from the SharedPreferences
Set:
SharedPref.setDefaults("your_tag", "your_string_you_want_to_put", context);
Get:
SharedPref.getDefaults("your_tag", getApplicationContext())
So, you can set it in to your textview like this (you should consider doing a null check or try/catch block as well):
tv.setText(SharedPref.getDefaults("your_tag", getApplicationContext()));
(Will keep this in case it is needed but this is a one-way method)
Here is a bundle example of how you can achieve:
In your Activity1:
Intent i = new Intent(Activity1.this,Activity2.class);
i.putExtra("string_tag", "string_value");
startActivity(i);
In your Activity2:
TextView tv = (TextView)findViewById(R.id.yourView);
tv.setText(getIntent().getStringExtra("string_tag"));
Upvotes: 1
Reputation: 1017
I do not share the thoughts on the previous poster. I would've done this:
Start Activity with an expected result:
Intent i = new Intent(CurrentActivity.this, SecondActivity.class);
i.putIntExtra("intToPass", 5);
startActivityForResult(i, REQUEST_CODE);
From SecondActivity pass the result back to Activity 1 on finish:
@Override
public void finish() {
// Prepare data intent
Intent data = new Intent();
data.putExtra("returnValue", returnValue);
// Activity finished ok, return the data
setResult(RESULT_OK, data);
super.finish();
Back in first Activity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("returnValue")) {
Toast.makeText(this, data.getExtras().getString("returnValue"),
Toast.LENGTH_SHORT).show();
//Do something with your precious return value ;)
}
}
}
If you need to save it for later use, then use sharedpreferences
Edit: REQUEST_CODE & RESULT_OK are your own defined ints
Upvotes: 0
Reputation: 1217
You have to maintain your data in public variables or sharedpreferences. Once you the activity is resumed you can get the value from the public variable or from sharedpreferences and then you can update textview.
Note: Read about activity Lifecycle that will help you to finish that.
Upvotes: 2
Reputation: 159
You can also send your value in an Intent like this:
public void sendMessage(View view) {
Intent intent = new Intent(this, Activity2.class);
EditText editText = (EditText) findViewById(R.id.whatever);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
and receive it:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
Upvotes: 1