How to send data from a second activity the first

0

I need help, I have the following part of the code:

 public void BotonAceptar (View vista){
        Intent intent= new Intent (DadesPersonals.this, EAC1.class);
        startActivity(intent);
    }

Well, I had thought about doing something like this:

 Adreça = (EditText) findViewById( R.id.Adreça );
 Adreça.setVisibility(View.VISIBLE);
 String adreça=Adreça.getText().toString();

But I get an error.

The program has to do the following:

You have to go back to the EAC1 Class and show me the screen up to this point, but there is just a button that Adreça is hidden and I need to show it.

To explain a little more the operation: My program goes through the EAC screen, I put the name if the program receives a name then unlocks the Adreça button, I can edit it. When I give it to edit it happens to another class that is called Dades. In Dades once I enter the value Adreça and I give Accept takes me to the previous page but of course with the value of Adreça unlocked (ie visible).

How can I do it? thanks.

    
asked by Montse Mkd 25.09.2017 в 23:35
source

2 answers

2

You have to use the starActivityForResult method. This is so that when you navigate from the second activity to the first, the first one knows what to do.

For example, in your first activity you launch the second one in the following way:

   Intent intent = new Intent(EAC.this, Adreca.class);
   startActivityForResult(intent, 11);

As we indicated the second activity we expect a result of it, then now, to send the data from the second to the first indicating that unblock the buton would be like this:

Intent resultado = new Intent(); // aqui especificamos los datos que queremos enviar la primera
resultado.putExtra("desbloquear_boton", true);
 setResult(Activity.RESULT_OK, resultado);
 finish();

Then to receive the parameters in the first one, it will be like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 11) {
        if(resultCode == Activity.RESULT_OK){
            boolean desbloquear=data.getBooleanExtra("desbloquear_boton", false);
            if(desbloquear) {
               // desbloquas el boton
            }

        }
        if (resultCode == Activity.RESULT_CANCELED) {
            // la respuesta fue cancelada, no desbloquear el boton
        }
    }
}//
    
answered by 26.09.2017 / 15:30
source
1

The problem I see is that DadesPersonals inherits EAC1 .
You have to create a chain in EAC1

 public static final String MI_DATA = "mi_data";

In oncreate () of the EAC1 class, place the following:

    try {

        String  accion = getIntent().getStringExtra(MI_DATA);

        if (accion.equals("mostrar")) {
            mostrarBotones();
        } else {
            ocultarBotones();
        }
    }catch(Exception e){
        Log.v("Data","Error");
    }

Create the showButton () and hideButton () methods. The first time it is created in the absence of the call will happen through the error.

private void ocultarBotones(){
    Log.v("Data","Ocultar Botones");
}

private void mostrarBotones(){
    //Aca coloque las acciones para mostrar los botones
    Log.v("Data","Mostrar Botones");
}

Create the two methods for the buttons that throw you error.

public void BotonAceptar (View vista){

    Intent intent= new Intent (getBaseContext(), EAC1.class);
    intent.putExtra(MI_DATA,"mostrar");
    startActivity(intent);

}
public void BotonCancelar (View vista){
    Intent intent= new Intent (getBaseContext(), EAC1.class);
    intent.putExtra(MI_DATA,"ocultar");
    startActivity(intent);
}

and place the actions you want to perform when pressed.

In the class DadesPersonals overwrite the methods of the parent class and leave them empty.

public void BotonAceptar (View vista){

}
public void BotonCancelar (View vista){

}

This causes calling DadesPersonal to ButtonAccept and ButtonCancel from the onClick property of the buttons execute the actions but in EAC1 since it is executing the methods of the parent class.

    
answered by 26.09.2017 в 19:35