How to transfer data of EditText to TextView to another activity and not lose it’s data in Android Studio using Java

  android-studio, java

So I’ve made two textedits. When you click "submit" I want to transfer it to another activity as TextView. I’ve managed to do this, but only problem is that, when I go to another activity and revisit this activity, those textviews that I’ve changed it with "submit" button are lost. It just says "null". Can some one help me to fix it. So it can just stay there and not lose it’s data. Here’s my java 1Activity code:

public class TwoTeam extends AppCompatActivity {

    private EditText firstName, secondName;
    private String first_name, second_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two_team);
        firstName = findViewById(R.id.first_name);
        secondName = findViewById(R.id.second_name);
    }

    public void submit_button(View view) {
            first_name = firstName.getText().toString().trim();
            second_name = secondName.getText().toString().trim();
            Intent intent = new Intent(this, Begin.class);{
                intent.putExtra(Begin.FIRST,first_name);
                intent.putExtra(Begin.SECOND,second_name);
                startActivity(intent);
                finish();
            }
        }
     }

And here’s my 2Activity code:

public class Begin extends AppCompatActivity {

    public static final String FIRST = "FIRSTTEAM";
    public static final String SECOND = "SECONDTEAM";
    private TextView first, second;
    private String first_name, second_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begin);
        first = findViewById(R.id.name_first);
        second = findViewById(R.id.name_second);

        Intent intent = getIntent();
        first_name = intent.getStringExtra(FIRST);
        second_name = intent.getStringExtra(SECOND);

        turn.setText(first_name +"'s Turn");
        first.setText(first_name +":");
        second.setText(second_name +":");
    }

Source: Android Studio Questions

LEAVE A COMMENT