How do I send multiple values to a single Activity

Asked

Viewed 262 times

2

Good morning, everyone,

I have the following situation:

I have 4 screens (Activity), they are: Mainactivity, Disiplinaactivity, Avaliacaoactivity and Resultfinalactivity.

The point is, the data collected from the Disiplinaactivity and Avaliacaoactivity screens is returned to Mainactivity and then sent to Resultadofinalactivity.

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

Has anyone ever taken something like this or can give me a light?

Thanks

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 Screen Button Disciplinaactivity

public void btnSalvar(View v) { Intent Intent = new Intent(this, Mainactivity.class); Intent.putExtra("nomenDiscipline", editnomeDisciplina.gettext().toString()); Intent.putExtra("nomeProfessor", editNomeProfessor.gettext().toString()); startActivity(Intent); setResult(RESULT_OK,Intent); Finish(); }

  • I didn’t understand this switch. It looks like it has a button for 4 screens. Are they unique data? Just one subject, just one teacher? Or are there several disciplines and various other data?

  • Exactly, I have 4 screens and this switch I used to redirect through the Activitys ID. For example, the Disciplinaactivity screen has the "Iscipline" and "Ofessor" fields, by clicking for example on the "Save" button it returns to Mainactivity already with the data in an Intent, right? So it goes to the screen of Avaliacaoactivity. After all, I have data of the two Activitys in Mainactivity and I need to send it to the final result screen using Intent.. I don’t know if I was clear, rsrsr

  • I still don’t understand, at the end of onClick from the save button you put extras in the Intent and calls the finish()?

  • I put the save button code

  • So it would be this flow: MainActivity.class > DisciplinaActivity.class > MainActivity.class > AvaliacaoActivity.class > MainActivity.class?

1 answer

1


How do I send this data via key,?

Just like a Bundle ago?

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 this way:

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

iniciarActivityBundle(bundle, ResultadoFinalActivity.class);

In Resultadofinalactivity, as soon as possible within the onCreate:

receberBundle(getIntent());

In Resultadofinalactivity, outside the 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();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.