How do I send multiple values to a single Activity

1

Good morning,

I have the following situation:

I have 4 screens (Activity), they are: MainActivity, DisiplinaActivity, AssessmentActivity, and Final ResultActivity.

The point is, the data collected from the DisiplinaActivity and EvaluationActivity screens return to the MainActivity and then are sent to the Final ResultActivity.

My question is: how do I send this data via key, value? So far I have this code, where I get the values, but I do not know how to send them to the final screen.

Has anyone caught something like this or can you give me a light?

Thank you

public void proximaTela(View v) { 
    Intent enviarDados = null;   

    switch (v.getId()) {
        case R.id.btnDisciplina:
            enviarDados = new Intent(this, DisciplinaActivity.class);
            enviarDados.putExtra("disciplina", disciplina);
            startActivityForResult(enviarDados, Constantes.REQUEST_CODE_DADOS_DISCIPLINA);
            break;

        case R.id.btnAvalicao1:
            enviarDados = new Intent(this, AvaliacaoActivity.class);
            enviarDados.putExtra("avaliacao1", disciplina);
            startActivityForResult(enviarDados, Constantes.REQUEST_CODE_DADOS_AVALIACAO);
            break;

        case R.id.btnAvalicao2:
            enviarDados = new Intent(this, AvaliacaoActivity.class);
            enviarDados.putExtra("avaliacao2", disciplina);
            startActivityForResult(enviarDados, Constantes.REQUEST_CODE_DADOS_AVALIACAO2);
            break;

        case R.id.telaResultado:
            enviarDados = new Intent(this, ResultadoActivity.class);
            break;

        default:
            Toast.makeText(this, "Problema ao enviar ou receber dados!", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK){
        if (requestCode == Constantes.REQUEST_CODE_DADOS_DISCIPLINA){
            Bundle params = data.getExtras();
            String disciplina = params.getString("nomeDisciplina");
            String professor = params.getString("nomeProfessor");
        }else if (requestCode == Constantes.REQUEST_CODE_DADOS_AVALIACAO){
            Bundle params = data.getExtras();
            String avaliacao1 = params.getString("tituloAvl1");
            Double nota1 = params.getDouble("notaAvl1");
        }else if (requestCode == Constantes.REQUEST_CODE_DADOS_AVALIACAO2){
            Bundle params = data.getExtras();
            String avalicao2 = params.getString("tituloAvl2");
            Double nota2 = params.getDouble("notaAvl2");
        }
    }

}

Save button from the DisciplineActivity screen

public void btnSave (View v) {         Intent intent = new Intent (this, MainActivity.class);         intent.putExtra ("className", editNameDiscipline.getText (). toString ());         int.extra ());         startActivity (intent);         setResult (RESULT_OK, intent);         finish ();     }

    
asked by anonymous 28.05.2018 / 14:22

1 answer

1
  

How do I send this data via key, value?

Just like a Bundle does?

private void iniciarActivityBundle(Bundle bundle, Class<?> cls) {
    // Supondo que este método está em uma activity, this é o contexto
    Intent intent = new Intent(this, cls);
    intent.putExtras(bundle);
    startActivity(intent);
}

Use it when passing data to an activity like this:

Bundle bundle = new Bundle();
bundle.putString("nomeDisciplina", "valor");
//bundle.putString("chave", "valor");
//bundle.putInt("chave", 0);
// ...

iniciarActivityBundle(bundle, ResultadoFinalActivity.class);

In the FinalActivity result, as soon as possible within onCreate :

receberBundle(getIntent());

On FinalActivity, outside of onCreate , declare:

private void receberBundle(Intent intent) {
    Bundle extras = intent.getExtras();

    if (extras != null) {
        String nomeDisciplina = extras.getString("nomeDisciplina");
        if (!TextUtils.isEmpty(nomeDisciplina)) {
            // ...
        }
    } else {
        // Não recebemos dados
        // Se for possível continuar a activity, faça, se não, aborte com finish();
    }
}
    
28.05.2018 / 19:54