Problem when modifying a textView that is in another form

0

I have a problem when changing the text of a texView found in the first layout by pressing a button that is in the second layout .

The thing is that if you let me change it backwards (from the first layout to the second one).

I enclose the two MainActivity I use:

public class MainActivity extends AppCompatActivity {

    Button siguiente;
    static EditText nombre;
    static TextView decision;

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

        siguiente =(Button)findViewById(R.id.VerificarBtn);
        nombre =(EditText)findViewById(R.id.NombreEt);
        decision =(TextView) findViewById(R.id.ResultadoTv);

        siguiente.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent siguiente = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(siguiente);
            }
        });
    }
}
public class Main2Activity extends AppCompatActivity {

    public TextView saludo;
    Button Aceptar;
    Button Rechazar;


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

        Aceptar =(Button)findViewById(R.id.AceptarBtn);
        Rechazar =(Button)findViewById(R.id.RechazarBtn);
        saludo =(TextView)findViewById(R.id.SaludoTv);

        saludo.setText("Hola "+MainActivity.nombre.getText().toString()+ ", ¿Aceptas las condiciones?");

        Aceptar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent Aceptar = new Intent(Main2Activity.this, MainActivity.class);
                startActivity(Aceptar);
                MainActivity.decision.setText("Condiciones Aceptadas");

            }
        });

        Rechazar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent Rechazar = new Intent(Main2Activity.this, MainActivity.class);
                startActivity(Rechazar);
                MainActivity.decision.setText("Condiciones Rechazadas");

                }
        });
    }
}
    
asked by Patata 01.12.2018 в 01:16
source

1 answer

0

After you started Main2Activity you can no longer reference variables declared in MainActivity .

For something simple like this, from MainActivity you can call startActivityForResult() in luar of startActivity() , and then from Main2Activity return to MainActivity returning the chosen option.

To get the result you have to implement the callback onActivityResult() in MainActivity() .

In the code below I made some changes:
1. I made the instance attributes by removing the static modifier | 2. The names of the attributes by convention I wrote in lowercase 3. Activity1ForResult becomes MainActivity .
4. Activity2ForResult becomes Main2Activity .
5. Remove the references from Main2Activity to Main1Activity variables.
6. To display the name in Main2Activity , I included the name in Intent that is sent from MainActivity .

Activity1ForResult (MainActivity)

public class Activity1ForResult extends AppCompatActivity {

    static final int IDENTIFICADOR_PEDIDO_1 = 1;

    Button siguiente;
    EditText nombre;
    TextView decision;

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

        siguiente =(Button)findViewById(R.id.btnSiguiente);
        nombre =(EditText)findViewById(R.id.etNombre);
        decision =(TextView) findViewById(R.id.tvDecision);

        siguiente.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent siguiente = new Intent(Activity1ForResult.this, Activity2ForResult.class);
                siguiente.putExtra("nombre",nombre.getText().toString());
                startActivityForResult(siguiente, IDENTIFICADOR_PEDIDO_1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Procesar solo el resultado que corresponde al pedido que hicimos
        if (requestCode == IDENTIFICADOR_PEDIDO_1) {
            // Verificar que la llamada ocurrió sin error.
            if (resultCode == RESULT_OK) {
                String respuesta = data.getStringExtra("respuesta");

                // Acá si podes referenciar la variable decisión y usar respuesta.
                // para setear el valor

                Activity1ForResult.this.decision.setText(respuesta);

            }
        }
    }
}

Activity2ForResult (Main2Activity)

public class Activity2ForResult extends AppCompatActivity {

    Button aceptar, rechazar;
    TextView saludo;

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

        aceptar =(Button)findViewById(R.id.btnAceptar);
        rechazar =(Button)findViewById(R.id.btnRechazar);
        saludo =(TextView)findViewById(R.id.tvSaludo);

        Intent intentRecibido = getIntent();
        String nombre = "";
        if(intentRecibido != null){
            nombre = intentRecibido.getStringExtra("nombre");
        }

        saludo.setText("Hola "+ nombre + ", ¿Aceptas las condiciones?");

        aceptar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent data = new Intent();
                data.putExtra("respuesta", "Condiciones Aceptadas");
                setResult(RESULT_OK, data);
                finish();
            }
        });

        rechazar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent data = new Intent();
                data.putExtra("respuesta", "Condiciones Rechazadas");
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }
}
    
answered by 01.12.2018 / 01:41
source