0
I have a form with 3 Fragments, where at last there is a save button, when I click on this button I need to take the data entered by the user in Fragment 1 and 2, get the data from the open Fragment (3) and save, for this I thought to use Singleton where I have a model class that contains all the values of the inputs, however I do not know how to save the data in this Singleton when I exit (Swipe or click on another Tab) of Fragment 1 and 2. Someone suggests me some solution please.
Function of saving data in Activity through the Fragment interface "A":
@Override
public void salvarDadosPessoais(HashMap<String, Object> hashMap) {
paciente.setNOME(hashMap.get("nome").toString());
paciente.setDATA_NASC(Utils.obterInstancia().StringToDate(hashMap.get("dataNasc").toString()));
paciente.setSEXO(hashMap.get("sexo").toString());
paciente.setTIPO_SANGUINEO(Integer.parseInt(hashMap.get("tipoSanguineo").toString()));
paciente.setNOME_MAE(hashMap.get("nomeMae").toString());
paciente.setDESCONHECE_NOME_PAI(Integer.parseInt(hashMap.get("desconhecePai").toString()));
paciente.setNOME_PAI(hashMap.get("nomePai").toString());
paciente.setEMAIL(hashMap.get("email").toString());
paciente.setSTATUS_NACIONALIDADE(Integer.parseInt(hashMap.get("statusNacionalidade").toString()));
paciente.setNATURALIDADE(hashMap.get("naturalidade").toString());
paciente.setCODIGO_DOCUMENTO(Integer.parseInt(hashMap.get("codigoDocumento").toString()));
paciente.setDATA_NATURALIZACAO(Utils.obterInstancia().StringToDate(hashMap.get("dataNaturalizacao").toString()));
paciente.setPORTARIA_NATURALIZACAO(hashMap.get("portariaNaturalizacao").toString());
paciente.setDATA_ENTRADA_BRASIL(Utils.obterInstancia().StringToDate(hashMap.get("dataEntradaBrasil").toString()));
paciente.setCODIGO_UF_NATURALIDADE(hashMap.get("codigoUfNaturalidade").toString());
paciente.setCODIGO_DOCUMENTO(Integer.parseInt(hashMap.get("codigoDocumento").toString()));
paciente.setDOCUMENTO(hashMap.get("documento").toString());
paciente.setCNS(hashMap.get("cns").toString());
paciente.setPRONTUARIO(hashMap.get("prontuario").toString());
paciente.setCPF(hashMap.get("cpf").toString());
paciente.setRG(hashMap.get("rg").toString());
paciente.setCTPS(hashMap.get("ctps").toString());
paciente.setCODIGO_RACA_COR(Integer.parseInt(hashMap.get("racaCor").toString()));
}
Function that generates Hashmap to send to Activity:
public HashMap<String, Object> persistTransients() {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("nome", cadNome.getText().toString());
hashMap.put("dataNasc", splitReverseJoin(cadDataNasc.getText().toString()));
hashMap.put("sexo", rootView.findViewById(cadRadGroupSexo.getCheckedRadioButtonId()).getTag().toString());
Integer tipoSanguineo = cadTipoSanguineo.getSelectedItemPosition();
hashMap.put("tipoSanguineo", getResources().getStringArray(R.array.Tipo_Sanguineo_Value)[tipoSanguineo]);
hashMap.put("nomeMae", cadNomeMae.getText().toString());
if(cadChkDescPai.isChecked()){
hashMap.put("desconhecePai", 1);
}else{
hashMap.put("desconhecePai", 0);
}
hashMap.put("nomePai", cadNomePai.getText().toString());
hashMap.put("email", cadEmail.getText().toString());
hashMap.put("statusNacionalidade", rootView.findViewById(cadRadGroupNac.getCheckedRadioButtonId()).getTag());
hashMap.put("naturalidade", cadMunicipioNasc.getTag().toString());
Integer paisN = cadPaisNasc.getSelectedItemPosition();
hashMap.put("codigoDocumento", getResources().getStringArray(R.array.Pais_Nasc_Value)[paisN]);
hashMap.put("dataNaturalizacao", splitReverseJoin(cadDataNatur.getText().toString()));
hashMap.put("portariaNaturalizacao", cadPortariaNatu.getText().toString());
hashMap.put("dataEntradaBrasil", Utils.obterInstancia().StringToDate(splitReverseJoin(cadDataEntrada.getText().toString())));
Integer estado = cadEstado.getSelectedItemPosition();
hashMap.put("codigoUfNaturalidade", getResources().getStringArray(R.array.Estados_Value)[estado]);
Integer tipoDoc = cadTipoDoc.getSelectedItemPosition();
hashMap.put("codigoDocumento", getResources().getStringArray(R.array.Tipo_Documento_Value)[tipoDoc]);
hashMap.put("documento", cadDocumento.getText().toString());
hashMap.put("cns", cadCns.getText().toString());
hashMap.put("prontuario", cadProntuario.getText().toString());
hashMap.put("cpf", cadCpf.getText().toString());
hashMap.put("rg", cadRg.getText().toString());
hashMap.put("ctps", cadCtps.getText().toString());
Integer racaCor = cadRacaCor.getSelectedItemPosition();
hashMap.put("racaCor", getResources().getStringArray(R.array.Raca_Cor_Value)[racaCor]);
return hashMap;
}
If you are going to use this information, provided by the user. Would it be better to store it locally? Depending on the amount of information I suggest the Sqlite, however you have more storage options.
– Rodrigo Paixão
Yes, where I say save is send to the local bank Sqlite, but in order to send to the local bank I need the information of all Fragments and do not know how to recover the information from Ragments that are not active.
– João Carlos
From what I understand, you have not yet created a database of Sqlite, correct. If not, there are some tutorials on how to do it. You can read more on this link. If you already have a database created, you can save the data every time you change Fragment. Or you could send everything through an interface, but that depends on how you’re building your application. If you can match what you’ve got ready by then...
– Rodrigo Paixão
I edited the question, necessarily I need a method to recognize when Fragment will be switched and call Activity’s method to save Fragment data in my Singleton Class, when I have the 3 complete Ragments I will have my Singleton Object complete and I will be able to save to the Sqlite database with all the information correctly.
– João Carlos