Pass objects between Android activities

1

I have an activity where a series of fields are filled. In it I have a button where when I press it opens a recyclerView, which presents the data of a table. When selecting one of the items in the RecyclerView, I want the object to be passed to the first activity and put the name attribute in an editext. How can I do to keep the data of the form of the first activity ?. This is how I have it but it does not work for me: Activity2:

            Proyecto proyectoseleccionado;
            proyectoseleccionado= (Proyecto)listaProyecto.get(recyclerViewProyectos.getChildAdapterPosition(v));
            //Muestro con un Toast el objeto seleccionado
            Toast.makeText(getApplicationContext(),proyectoseleccionado.getNombre(),Toast.LENGTH_SHORT).show();
            //Envío el objeto seleccionado a Registro Dietas
            Intent intent = new Intent(ListaProyectosActivity.this,RegistroDietasActivity.class);

            Bundle bundle= new Bundle();
            bundle.putSerializable("proyecto",proyectoseleccionado);

            intent.putExtras(bundle);
            startActivityForResult(intent,1);

Activity 1 where you receive the object:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bundle objetoRecibido = data.getExtras();
    Proyecto proyectoseleccionado=null;
    Usuario usuarioseleccionado =null;

    if (requestCode ==1 && requestCode==RESULT_OK){
        proyectoseleccionado = (Proyecto) objetoRecibido.getSerializable("proyecto");
        if (proyectoseleccionado !=null)
            txtProyecto.setText(proyectoseleccionado.getNombre().toString());
    }

}

The truth is that I do not know if I should do it with the ActivityResult ....

    
asked by Raúl Cuc 20.11.2018 в 20:15
source

2 answers

-1

I already got it

In the first activity I call the second one this way:

            Intent miIntent=new Intent(RegistroDietasActivity.this,ListaProyectosActivity.class);
            startActivityForResult(miIntent,1);

In the second activity when you select an Item, you send the result to the first one in this way:

            Proyecto proyectoseleccionado;
            proyectoseleccionado= (Proyecto)listaProyecto.get(recyclerViewProyectos.getChildAdapterPosition(v));
            //Muestro con un Toast el objeto seleccionado
            Toast.makeText(getApplicationContext(),proyectoseleccionado.getNombre(),Toast.LENGTH_SHORT).show();
            //Envío el objeto seleccionado a Registro Dietas
            Intent intent = new Intent(ListaProyectosActivity.this,RegistroDietasActivity.class);

            Bundle bundle= new Bundle();
            bundle.putSerializable("proyecto",proyectoseleccionado);

            intent.putExtras(bundle);
            setResult(RESULT_OK,intent);
            finish();

Again in the first activity I pick up the object in this way:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bundle objetoRecibido = data.getExtras();
    Proyecto proyectoseleccionado=null;
    Usuario usuarioseleccionado =null;
    //Toast.makeText(getApplicationContext(),"He llegado",Toast.LENGTH_SHORT).show();
    if (requestCode == 1 ){
        proyectoseleccionado = (Proyecto) objetoRecibido.getSerializable("proyecto");
        if (proyectoseleccionado !=null)
            txtProyecto.setText(proyectoseleccionado.getNombre().toString());
    }
    if (requestCode ==2 ){
        usuarioseleccionado = (Usuario) objetoRecibido.getSerializable("usuario");
        if (usuarioseleccionado !=null)
            txtUsuario.setText(usuarioseleccionado.getNombre().toString());

    }
}

This way when I do not delete any data from the form.

    
answered by 20.11.2018 / 21:04
source
1

Send object between Activities:

One option is to implement the class Serializable on your object:

public class Proyecto implements Serializable {

You would send an object in Intent using .putExtra() (it is not necessary to use startActivityForResult() ):

   Intent intent = new Intent(MainActivity.this, SegundaActivity.class);
                intent.putExtra("proyecto", proyectoseleccionado);
                startActivity(intent);

To receive the object in the Activity is done in this way, receiving it within the method onCreate() (NOT in onActivityResult() ):

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

     ...
     ...
     Proyecto  proyectoseleccionado = (Proyecto) getIntent().getSerializable("proyecto");
     ...
     ...

}
    
answered by 20.11.2018 в 20:51