Home > OS >  TextEdit values are passed to another activity but when revisiting next activity values are lost in
TextEdit values are passed to another activity but when revisiting next activity values are lost in

Time:09-22

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 {

    EditText first_name, second_name;
    String name_first, name_second;

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

        first_name = findViewById(R.id.first_name);
        second_name = findViewById(R.id.second_name);

    }

    public void submit_button(View view) {
        Intent intent = new Intent(TwoTeam.this, Begin.class);{
            name_first = first_name.getText().toString();
            name_second = second_name.getText().toString();
            intent.putExtra("name1", name_first);
            intent.putExtra("name2", name_second);
            startActivity(intent);
            finish();
        }
    }
}

And this is 2Activity code:

public class Begin extends AppCompatActivity {

    private TextView first_name, second_name;
    String name_first, name_second;

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

        name_first = getIntent().getExtras().getString("name1");
        first_name.setText(name_first);

        name_second = getIntent().getExtras().getString("name2");
        second_name.setText(name_second);
        
    }

CodePudding user response:

Why do you call finish() method after startActivity? If you use this method this class will be killed after Begin class started so you lose information on the TwoTeam class.

  • Related