I have a problem. I have 2 activities, the main one and a second screen. I want the 2nd screen to send data to the main screen but when I start the application it tells me that it stopped and it closes. When I do it the other way around (From the main page to the 2nd screen) it works without problems.
Does anyone know why the error? To pass the data I'm using Bundle, is it the correct way or is there a better one?
Thank you.
Main screen:
public class MainActivity extends AppCompatActivity {
TextView tv2;
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv2=(TextView)findViewById(R.id.tv2);
b2=(Button)findViewById(R.id.b2);
Bundle parametros = this.getIntent().getExtras();
String datos = parametros.getString("datos");
tv2.setText(datos);
}
public void segunda_pantalla(View view){
Intent i=new Intent(this, segunda_pantalla.class);
startActivity(i);
}
Second Screen:
public class pantalla2 extends AppCompatActivity {
Button b1;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantalla2);
b1=(Button)findViewById(R.id.b1);
tv1=(TextView)findViewById(R.id.tv1);
}
public void b1(View view){
tv1.setText("1");
String datos = tv1.getText().toString();
Bundle parmetros = new Bundle();
parmetros.putString("datos", datos);
Intent i = new Intent(this, MainActivity.class);
i.putExtras(parmetros);
startActivity(i);
}