简体   繁体   中英

carrying intent data throughout 2 activities in android studio

i carried some edittext string data from a second activity to the first and placed the string data into textviews in the first activity. this worked fine.

when i go from the first activity back to the second activity (done using the options menu), i want to carry the previously entered edittext data back into the edit texts in the second activity. i cant get it to carry over - the second activity edittexts go back to the original edittext settings (empty).

this is my second activity code:

public void goToMain(View view) { //run when a button is clicked
    Intent intent = new Intent(setPlayersActivity.this, MainActivity.class);
    intent.putExtra("P1",p1EditText.getText().toString());
    startActivity(intent);
}

and in my first activity code i have in the on create:

     Intent name = getIntent();
     p1TextView.setText(name.getStringExtra("P1"));

to go from the first activity to the second

public boolean onOptionsItemSelected(@NonNull MenuItem item) {
     if (item.getItemId() == R.id.resetAll) {
         updateScoreTeamA(scoreA = 0);
     } else if (item.getItemId() == R.id.setPlayers) {
         Intent intent = new Intent(MainActivity.this, setPlayersActivity.class);
         startActivity(intent);
     }
     return super.onOptionsItemSelected(item);
 }

up to here is working fine.

upon returning to the second activity though, the edittexts are again empty (i want to carry the previously entered data back to this activity).

to fill the edittexts with the previously entered string data, i tried the following in the on create of the second activity.

p1EditText.setText(getIntent().getStringExtra("P1"));

thanks in advance for the help. i am a beginner with programming and cannot imagine making any progress without this community.

When your changing activity you have to provide data to that certain intent. Otherwise it won't work

Intent intent = new Intent(MainActivity.this, setPlayersActivity.class);
intent.putExtra("P1","data");
startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM