I have a TextView in Activity 1 with the default string value "Select a location"
that when clicked, Activity 2 is opened and the user creates a string. Just before the Activity 2 finishes and returns to the Activity 1, I want that TextView (in Activity 1) to be updated with the new string value. My problem is that the TextView retains its default value and does not get updated with the new one.
I've also tried setting up SharedPreferences
, but it didn't work either. My current way of doing it is by inflating the layout of Activity 1 in Activity 2, and updating the text with an instance of the TextView as shown in the line street_address_textview.setText(chosenLocationString);
. I have excluded irrelevant parts of the code, and chosenLocationString
has the correct value.
Activity 1:
TextView streetAddress_textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
streetAddress_textview = (TextView) findViewById(R.id.StreetAddress_textview);
streetAddress_textview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
// Create a new intent to open the Set Event Location Activity
Intent intent = new Intent(CreateEventActivity.this,
SetEventLocationActivity.class);
// Start the new activity
startActivity(intent);
}
});
{
Activity 2:
TextView street_address_textview;
TextView set_location_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_event_location);
// Inflate Create Event activity in order to access street address textview
View inflatedView = getLayoutInflater().inflate(R.layout.activity_create_event, null);
street_address_textview = (TextView) inflatedView.findViewById(R.id.StreetAddress_textview);
set_location_button = (TextView) findViewById(R.id.set_location_button);
// When Set Location button is clicked, set street address textview, close activity
set_location_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(chosenLatLng != null) {
chosenLocationString = chosenLatLng.toString();
street_address_textview.setText(chosenLocationString);
SetEventLocationActivity.this.finish();
}
}
});
}
Thanks!