Search code examples
javaandroidandroid-activitytextviewupdating

Android Studio: TextView in Second Activity not Updating


I am trying to change the text of a TextView at runtime when the user taps a button. I call setText() from a private method in a Fragment which should update a TextView in an XML used by an Activity I have created. The Fragment is one of those generated by the Navigation Drawer Activity preset in case that's helpful. Here is the method inside the Fragment:

private void openGameActivity(List<Game> currentYearCategory, int gameNum){
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view = layoutInflater.inflate(R.layout.activity_game, null, false);

        TextView textView = view.findViewById(R.id.refereeAndDate);

        String string = "test string";
        textView.setText(string);

        Intent intent = new Intent(getActivity(), GameActivity.class);
        startActivity(intent);
    }

The activity opens correctly and there are no errors. findViewById is able to find the TextView and calling setText() must change the text because I have tried calling getText() on the TextView and it returns the updated value. The problem is that when I run the app the TextView text doesn't visually update. This is the activity code in case it is useful:

public class GameActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setSubtitle(R.string.page_game_details);
        }
    }

    public boolean onOptionsItemSelected(MenuItem item){
        finish();
        return true;
    }
}

The TextView XML code from the activity_game layout:

<TextView
    android:id="@+id/refereeAndDate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="The match was refereed by Brown on 16/04/12." />

I am sure that the ID of this TextView is not a duplicate. How to make it update?


Solution

  • Try to pass String from Fragment to Activity using Intent like below:

    private void openGameActivity(List<Game> currentYearCategory, int gameNum){
        ....
    
        String string = "test string";
        textView.setText(string);
    
        Intent intent = new Intent(getActivity(), GameActivity.class);
        intent.putExtra("SHARED_CONTENT", string);
        startActivity(intent);
    }
    

    Then in your GameActivity change like below:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ....
    
        TextView refereeAndDate = findViewById(R.id.refereeAndDate);
        String string = getIntent().getStringExtra("SHARED_CONTENT");
        refereeAndDate.setText(string);
    }