About the onActivityResult() method in Android Studio

Asked

Viewed 219 times

-1

Good afternoon guys, I have a job to deliver done in Android Studio, I was trying to send Strings by intents, my teacher said that the correct way would be using startActivityForResult() and Onactivity(), but it seems that the Strings are not being sent, I tried to change it in various ways. Can you help me?

Sending Activity Code

            btArmazenaDisco.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            estilMusica = EstiloMusical.getText().toString();
            alb = album.getText().toString();
            artist = artista.getText().toString();

                quantmusic = quantmusica.getText().toString();

                 // enviando os itens
                   Intent intentEnviador = new Intent(CriarDiscoActivity.this,Activity_Pastas.class);
                   Bundle informacao = new Bundle();



                    informacao.putString("addestilMusica",estilMusica);
                    informacao.putString("addAlbum",alb);
                    informacao.putString("addArtista",artist);
                    informacao.putString("addQuantiMusica",quantmusic);

                    intentEnviador.putExtras(informacao);
                    setResult(RESULT_OK,intentEnviador);


                   startActivityForResult(intentEnviador,Constante_Activity_Pastas);


            }

Code Activity Receiver

@Override

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if(resultCode==CriarDiscoActivity.RESULT_OK && requestCode== CriarDiscoActivity.Constante_Activity_Pastas) {

        Bundle bundle = data.getExtras();
        if(bundle!= null) {


            estiloMusica = bundle.getString("addestilMusica");
            Album = bundle.getString("addAlbum");
            Artista = bundle.getString("addArtista");
            QuantMusica = bundle.getString("addQuantiMusica");

                //Testando para ver se foi enviado
                test.setText(estiloMusica);
            Toast.makeText(this, estiloMusica, Toast.LENGTH_SHORT).show();
            //
            musicas.add(new Musica(estiloMusica, Album, Artista, QuantMusica, 0));
        }

    }


}
  • Put code as formatted text here, don’t use images to share code

  • Okay, I put the code, thank you for warning.

1 answer

0


It seems that you are confusing some things. A same Activity that uses startActivityForResult() should implement the method onActivityResult().

Initial Activity

btArmazenaDisco.setOnClickListener(new View.OnClickListener() {
    Intent intentEnviador = new Intent(CriarDiscoActivity.this, Activity_Pastas.class);
    Bundle informacao = new Bundle();

    informacao.putString("addestilMusica",estilMusica);
    informacao.putString("addAlbum",alb);
    informacao.putString("addArtista",artist);
    informacao.putString("addQuantiMusica",quantmusic);
    intentEnviador.putExtras(informacao);

    // Aqui você iniciará a Activity "Activity_Pastas"
    startActivityForResult(intentEnviador,Constante_Activity_Pastas);
}
// Aqui você receberá o retorno da Activity "Activity_Pastas", caso tenha algum.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == Constante_Activity_Pastas) {
        if(resultCode == Activity.RESULT_OK){
            // Faça algo
        }
    }
}

Called Activity

// Código para receber a informação passada
Intent recebido = getIntent()
String estilMusica = intent.getStringExtra("addestilMusica");
String alb = intent.getStringExtra("addAlbum");
String artist = intent.getStringExtra("addArtista");
String quantmusic = intent.getStringExtra("addQuantiMusica");

// Código para encerrar a Activity e avisar a Inicial (que está esperando pelos resultados)
Intent intent = new Intent();
setResult(Activity.RESULT_OK, intent);
finish();

Notice that in the example I did not pass any Extra from Activity Call to Initial Activity, but this can be done by placing the Extras in the Intent and receiving in onActivityResult.

In order, occurs:

  1. Click the button -> startActivityForResult();
  2. Activity_Pastas is created;
  3. You do what you need in Activity_Pastas, receives the data with getIntent();
  4. Warns that is closing the Activity_Pastas with setResult(Activity.RESULT_OK, intent);
  5. Closes the Activity_Pastas with finish();
  6. onActivityResult() is called in the main activity with requestCode == Constante_Activity_Pastas and resultCode == Activity.RESULT_OK.
  • Ah then onActivityResult() is placed in the Activity that I am sending the items, means to confirm this shipment?

  • Thank you so much for your help.

  • Yeah, not exactly to confirm the shipment, but to get something back. For example, you started the second Activity and want a return loop with some data. If you don’t want the return, you can use only startActivity, in documentation have a few examples

  • Just one question, my teacher asked to add this method, what I’m doing is just sending data to add in a list view, can you give me an example of what to do within this method?

  • What to do inside the onActivityResult? Without having the general context of the application, I can not know, but it seems to me that you do not need to do anything there, It is even difficult to give an example, but in general it is when your ActivityUm needs some feedback from ActivityDois to update the interface.

  • Got it, thank you very much.

  • Like my main Activity (the one that opens when I start the application) is the one from List View, then I enter it to store the data from a disk, then I go back to it again.

  • You can use the onActivityResult to display a Toast written "Stored data", for example

  • Got it, thank you very much.

Show 4 more comments

Browser other questions tagged

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