Cathal Cathal - 3 months ago 7
Android Question

String is blank when sending with Intents

I am trying to send a String from one activity to another using intents, however whenver I send it over and set a TextView to that string, the string is blank. Here is the Intent code:

String homeTeam = "temp";
String awayTeam = "temp";

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gaelic_param);

EditText homeTeamEditText = (EditText) findViewById(R.id.HomeTeam);
EditText awayTeamEditText = (EditText) findViewById(R.id.AwayTeam);

homeTeam = homeTeamEditText.getText().toString();
awayTeam = awayTeamEditText.getText().toString();
}

public void nextPage(View view)
{
Intent intent = new Intent(GaelicParam.this, GaelicActivity.class);
intent.putExtra("homeTeamExtra", homeTeam);
startActivity(intent);
}


Here is what I do to set the text.

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gaelic_main);

Bundle extras = getIntent().getExtras();
if(extras != null) {
String HomeTeam = extras.getString("homeTeamExtra");

TextView homeTextView = (TextView)findViewById(R.id.homeTeam_text_view);
homeTextView.setText(HomeTeam);
}
}


However the textView is still empty.

Answer

use this code put homeTeam = homeTeamEditText.getText().toString(); inside so it will take latest value of editText

public void nextPage(View view){
homeTeam = homeTeamEditText.getText().toString();
Intent intent = new Intent(GaelicParam.this, GaelicActivity.class);
intent.putExtra("homeTeamExtra", homeTeam);
startActivity(intent);
}