Search code examples
androidandroid-activitytextview

TextView not being updated from other activity


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!


Solution

  • Inflating a view doesn't mean that it is visible unless you pass a parent View object and attachToRoot true while inflating. In your above snippet you are just instantiating a new View object using LayoutInflater. But it's not the same View which is being displayed in the Activity1 rather just another instance of the same View. There are several way you can achieve your goal but I think the following way makes sense in your case. Firstly, you have to start your Activity2 by calling startActivityForResult() method instead of startActivity() and getting the result in onActivityResult() . Here is an example,

    Activity 1:

    private static final int YOUR_REQUEST = 123;
    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
                    startActivityForResult(intent, YOUR_REQUEST);
                }
            });
         }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // Check which request we're responding to
            if (requestCode == YOUR_REQUEST) {
                // Make sure the request was successful
                if (resultCode == RESULT_OK) {
                    String returnString = data.getStringExtra("result");
                    streetAddress_textview.setText(returnString);
                }
            }
        }
    

    Activity 2:

    TextView set_location_button;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_set_event_location);
    
            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();
                        Intent returnIntent = new Intent();
                        returnIntent.putExtra("result", chosenLocationString);
                        setResult(Activity.RESULT_OK,returnIntent);
                        SetEventLocationActivity.this.finish();
                    }
                }
            });
        }
    

    I hope the above solution will solve your problem. Happy coding :)