How can I go from one activity to another without losing data?

0

In my RegistroPedidoActivity I have two buttons that take me to two Activities: SeleccionProductoActivity and SeleccionDatoActivity and SeleccionClienteActivity . First I must go to SeleccionClienteActivity , with the data obtained from this activity I must go to SeleccionDatoActivity , but when I return to the main to go to SeleccionProductoActivity , the data that was obtained at the beginning is lost. How can I make this not happen?

This is my code:

public class RegistroPedidoActivity extends AppCompatActivity {

private TextView btnSeleccionar;
private ImageView btnGuardarP;
private ImageView btnItems;
private ImageView btnDatos;
private TextView txtTotal;
private TextView txtDescuento;
private TextView txtIGV;
private TextView txtISC;
private String fecha;
private ListView lvCliente;
private List<CondicionPago> listaPago=new ArrayList<CondicionPago>();
private List<TipoFactura> listaFactura=new ArrayList<TipoFactura>();
private Spinner sCondicionPago;
private Spinner sTipoFactura;
private EditText etTelefono;


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

    btnSeleccionar = findViewById(R.id.btnSiguiente);
    txtTotal = findViewById(R.id.etTotalP);
    txtDescuento = findViewById(R.id.etDescuento);
    txtIGV = findViewById(R.id.etIGV);
    txtISC = findViewById(R.id.etISC);
    etTelefono = findViewById(R.id.etTelefono);
    btnDatos = findViewById(R.id.btnDatos);
    btnItems =findViewById(R.id.btnItems);
    btnGuardarP = findViewById(R.id.btnGuardarPedido);
    sCondicionPago = findViewById(R.id.sPago);
    sTipoFactura = findViewById(R.id.sTipoFactura);
    lvCliente = findViewById(R.id.lvClienteList);


    getSupportActionBar().hide();


    int telefono= getIntent().getIntExtra("telefonoCliente", 0);
    String cliente = getIntent().getStringExtra("nombreCliente");
    if(cliente==null){
        etTelefono.setText("TELEFONO");
        btnSeleccionar.setText("NOMBRE DEL CLIENTE");
    }else {
        etTelefono.setText(String.valueOf(telefono));
        btnSeleccionar.setText(cliente);
    }

    double total = getIntent().getDoubleExtra("total", 0.00);
    double descuento = getIntent().getDoubleExtra("descuento", 0.00);
    double isc = getIntent().getDoubleExtra("isc", 0.00);
    double igv = getIntent().getDoubleExtra("igv", 0.00);

    if(total==0.00 && descuento==0.00 && isc==0.00 && igv==0.00){
        txtTotal.setText("TOTAL");
        txtDescuento.setText("DESCUENTO");
        txtISC.setText("ISC");
        txtIGV.setText("IGV");
    }else {
        txtTotal.setText(String.valueOf(total));
        txtDescuento.setText(String.valueOf(descuento));
        txtISC.setText(String.valueOf(isc));
        txtIGV.setText(String.valueOf(igv));
    }

    llenarSpinnerCondicionPago();
    llenarSpinnerTipoFactura();

    sCondicionPago.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            CondicionPago item = (CondicionPago) parent.getItemAtPosition(position);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    sTipoFactura.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            TipoFactura item = (TipoFactura) parent.getItemAtPosition(position);
        }


        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    btnSeleccionar.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(RegistroPedidoActivity.this, SeleccionClienteActivity.class);
            startActivity(intent);
        }
    });


    btnItems.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(RegistroPedidoActivity.this, SeleccionProductoActivity.class);
            startActivity(intent);
        }
    });

    btnDatos.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            String direccionEntrega = getIntent().getStringExtra("direccionEntrega");
            Intent intent = new Intent(RegistroPedidoActivity.this, SeleccionDatosActivity.class);
            intent.putExtra("direccionEntrega", direccionEntrega);
            startActivity(intent);
        }
    });



}

private void llenarSpinnerCondicionPago() {
    RequestQueue queue = Volley.newRequestQueue(this);
    final String url="http://localhost:100/Coop/public/index.php/api/CondicionPago";

    JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    try {

                        listaPago.add(new CondicionPago(0, "Seleccione"));
                        for (int i = 0; i < response.length(); i++) {
                            JSONObject value = response.getJSONObject(i);
                            listaPago.add(new CondicionPago(value.getInt("id_condicion_pago"), value.getString("descripcion_condicion_pago")));
                        }
                        final ArrayAdapter<CondicionPago> adapter = new ArrayAdapter<CondicionPago>(RegistroPedidoActivity.this, R.layout.support_simple_spinner_dropdown_item, listaPago);
                        sCondicionPago.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error.Response", error.getMessage());
        }
    });

    queue.add(getRequest);
}


private void llenarSpinnerTipoFactura() {
    RequestQueue queue = Volley.newRequestQueue(this);
    final String url="http://10.51.1.109:100/Coop/public/index.php/api/TipoFactura";

    JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    try {

                        listaFactura.add(new TipoFactura(0, "Seleccione"));
                        for (int i = 0; i < response.length(); i++) {
                            JSONObject value = response.getJSONObject(i);
                            listaFactura.add(new TipoFactura(value.getInt("cod_tipo_fact"), value.getString("nom_tipo_fact")));
                        }
                        final ArrayAdapter<TipoFactura> adapter = new ArrayAdapter<TipoFactura>(RegistroPedidoActivity.this, R.layout.support_simple_spinner_dropdown_item, listaFactura);
                        sTipoFactura.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error.Response", error.getMessage());
        }
    });

    queue.add(getRequest);
}
}
}

Thank you.

    
asked by Marisol Gutierrez Flores 05.09.2018 в 02:47
source

1 answer

2

You can achieve this by using startActivityForResult . In essence, what it does is that it starts an activity waiting for a result of it.

For example, when you navigate, you would do this:

Intent intent = new Intent(RegistroPedidoActivity.this, SeleccionDatosActivity.class);
intent.putExtra("direccionEntrega", direccionEntrega);
startActivityForResult(intent, 123);

Then when you navigate back, you assign the result using the setResult() :

SeleccionDatosActivity.java:

Intent returnIntent = new Intent();
returnIntent.putInt("numeroTelefono", 1);
returnIntent.putExtra("resultado",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();

Then to receive the data in the activity that I call SeleccionDatosActivity , you overwrite the method onActivityResult :

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

    // comprobamos que el codigo de peticion sea el mismo definido arriba
    if (requestCode == 123) {
        if(resultCode == Activity.RESULT_OK){
             // lees los datos del parametro data
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}
    
answered by 05.09.2018 в 14:21