I'm not getting data from another activity











up vote
-1
down vote

favorite












I am getting a problem with my project in Android Studio.
What I am trying to do is when I click a button (what opens another activity) and in that activity I write data from a person.
Then I save this data and the app get back to the main. The next button what does is create a Toast that shows data from the person, but the Toast is returning me null. I don't know what I am doing wrong.



   public class PrincipalActivity extends AppCompatActivity implements View.OnClickListener {

Button btnObreActivitat1, btnToastDadesPersona;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);

btnObreActivitat1 = findViewById(R.id.btnObreActivitat1);
btnObreActivitat1.setOnClickListener(this);
btnToastDadesPersona = findViewById(R.id.btnToastDadesPersona);
btnToastDadesPersona.setOnClickListener(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Resposta invalida", Toast.LENGTH_SHORT);
} else {
switch (requestCode) {
case 5:
setTitle(data.getStringExtra("RESPOSTA"));
break;
}
}
}

@Override
public void onClick(View v) {
Intent intencio;
StringBuilder sbUri = new StringBuilder();

switch (v.getId()){
case R.id.btnObreActivitat1:
intencio = new Intent(this,Activitat1.class);
startActivityForResult(intencio,5);
break;
case R.id.btnToastDadesPersona:
intencio = getIntent();
String nom = intencio.getStringExtra("NOM");
String cognom = intencio.getStringExtra("COGNOM");
String sexe = intencio.getStringExtra("SEXE");

Toast torrada = new Toast(this);
LayoutInflater inflador = getLayoutInflater();
View vista = inflador.inflate(R.layout.torrada,null);
TextView tvMissatge = vista.findViewById(R.id.tvMissatge);
tvMissatge.setText("Nom: " + nom + "nCognom: " + cognom + "nSexe: " + sexe);

torrada.setView(vista);
torrada.setDuration(Toast.LENGTH_SHORT);
torrada.show();
break;
}
}

@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume(){
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
}


//Next activity where I write data :



 public class Activitat1 extends AppCompatActivity {
final static String nom = "NOM";
final static String cognom = "COGNOM";
final static String sexe = "SEXE";

Button btnEnviar, btnCancelar;
EditText etNom, etCognom;
RadioButton rbtSexe;
RadioGroup rgpSexe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitat1);

etNom = findViewById(R.id.etNom);
etCognom = findViewById(R.id.etCognom);
btnEnviar = findViewById(R.id.btnEnviar);
btnCancelar = findViewById(R.id.btnCancelar);

rgpSexe = findViewById(R.id.rgpSexe);
rbtSexe = findViewById(rgpSexe.getCheckedRadioButtonId());
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
startActivity(intencio);
}
});

btnCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}

});
}
}









share|improve this question




























    up vote
    -1
    down vote

    favorite












    I am getting a problem with my project in Android Studio.
    What I am trying to do is when I click a button (what opens another activity) and in that activity I write data from a person.
    Then I save this data and the app get back to the main. The next button what does is create a Toast that shows data from the person, but the Toast is returning me null. I don't know what I am doing wrong.



       public class PrincipalActivity extends AppCompatActivity implements View.OnClickListener {

    Button btnObreActivitat1, btnToastDadesPersona;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_principal);

    btnObreActivitat1 = findViewById(R.id.btnObreActivitat1);
    btnObreActivitat1.setOnClickListener(this);
    btnToastDadesPersona = findViewById(R.id.btnToastDadesPersona);
    btnToastDadesPersona.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (resultCode == RESULT_CANCELED) {
    Toast.makeText(this, "Resposta invalida", Toast.LENGTH_SHORT);
    } else {
    switch (requestCode) {
    case 5:
    setTitle(data.getStringExtra("RESPOSTA"));
    break;
    }
    }
    }

    @Override
    public void onClick(View v) {
    Intent intencio;
    StringBuilder sbUri = new StringBuilder();

    switch (v.getId()){
    case R.id.btnObreActivitat1:
    intencio = new Intent(this,Activitat1.class);
    startActivityForResult(intencio,5);
    break;
    case R.id.btnToastDadesPersona:
    intencio = getIntent();
    String nom = intencio.getStringExtra("NOM");
    String cognom = intencio.getStringExtra("COGNOM");
    String sexe = intencio.getStringExtra("SEXE");

    Toast torrada = new Toast(this);
    LayoutInflater inflador = getLayoutInflater();
    View vista = inflador.inflate(R.layout.torrada,null);
    TextView tvMissatge = vista.findViewById(R.id.tvMissatge);
    tvMissatge.setText("Nom: " + nom + "nCognom: " + cognom + "nSexe: " + sexe);

    torrada.setView(vista);
    torrada.setDuration(Toast.LENGTH_SHORT);
    torrada.show();
    break;
    }
    }

    @Override
    protected void onStart() {
    super.onStart();
    }
    @Override
    protected void onResume(){
    super.onResume();
    }
    @Override
    protected void onPause() {
    super.onPause();
    }
    @Override
    protected void onStop() {
    super.onStop();
    }
    }


    //Next activity where I write data :



     public class Activitat1 extends AppCompatActivity {
    final static String nom = "NOM";
    final static String cognom = "COGNOM";
    final static String sexe = "SEXE";

    Button btnEnviar, btnCancelar;
    EditText etNom, etCognom;
    RadioButton rbtSexe;
    RadioGroup rgpSexe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activitat1);

    etNom = findViewById(R.id.etNom);
    etCognom = findViewById(R.id.etCognom);
    btnEnviar = findViewById(R.id.btnEnviar);
    btnCancelar = findViewById(R.id.btnCancelar);

    rgpSexe = findViewById(R.id.rgpSexe);
    rbtSexe = findViewById(rgpSexe.getCheckedRadioButtonId());
    btnEnviar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intencio = Activitat1.this.getIntent();
    intencio.putExtra(nom, etNom.getText().toString());
    intencio.putExtra(cognom, etCognom.getText().toString());
    intencio.putExtra(sexe,rbtSexe.getText().toString());
    startActivity(intencio);
    }
    });

    btnCancelar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    setResult(RESULT_CANCELED);
    finish();
    }

    });
    }
    }









    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I am getting a problem with my project in Android Studio.
      What I am trying to do is when I click a button (what opens another activity) and in that activity I write data from a person.
      Then I save this data and the app get back to the main. The next button what does is create a Toast that shows data from the person, but the Toast is returning me null. I don't know what I am doing wrong.



         public class PrincipalActivity extends AppCompatActivity implements View.OnClickListener {

      Button btnObreActivitat1, btnToastDadesPersona;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_principal);

      btnObreActivitat1 = findViewById(R.id.btnObreActivitat1);
      btnObreActivitat1.setOnClickListener(this);
      btnToastDadesPersona = findViewById(R.id.btnToastDadesPersona);
      btnToastDadesPersona.setOnClickListener(this);
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      if (resultCode == RESULT_CANCELED) {
      Toast.makeText(this, "Resposta invalida", Toast.LENGTH_SHORT);
      } else {
      switch (requestCode) {
      case 5:
      setTitle(data.getStringExtra("RESPOSTA"));
      break;
      }
      }
      }

      @Override
      public void onClick(View v) {
      Intent intencio;
      StringBuilder sbUri = new StringBuilder();

      switch (v.getId()){
      case R.id.btnObreActivitat1:
      intencio = new Intent(this,Activitat1.class);
      startActivityForResult(intencio,5);
      break;
      case R.id.btnToastDadesPersona:
      intencio = getIntent();
      String nom = intencio.getStringExtra("NOM");
      String cognom = intencio.getStringExtra("COGNOM");
      String sexe = intencio.getStringExtra("SEXE");

      Toast torrada = new Toast(this);
      LayoutInflater inflador = getLayoutInflater();
      View vista = inflador.inflate(R.layout.torrada,null);
      TextView tvMissatge = vista.findViewById(R.id.tvMissatge);
      tvMissatge.setText("Nom: " + nom + "nCognom: " + cognom + "nSexe: " + sexe);

      torrada.setView(vista);
      torrada.setDuration(Toast.LENGTH_SHORT);
      torrada.show();
      break;
      }
      }

      @Override
      protected void onStart() {
      super.onStart();
      }
      @Override
      protected void onResume(){
      super.onResume();
      }
      @Override
      protected void onPause() {
      super.onPause();
      }
      @Override
      protected void onStop() {
      super.onStop();
      }
      }


      //Next activity where I write data :



       public class Activitat1 extends AppCompatActivity {
      final static String nom = "NOM";
      final static String cognom = "COGNOM";
      final static String sexe = "SEXE";

      Button btnEnviar, btnCancelar;
      EditText etNom, etCognom;
      RadioButton rbtSexe;
      RadioGroup rgpSexe;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_activitat1);

      etNom = findViewById(R.id.etNom);
      etCognom = findViewById(R.id.etCognom);
      btnEnviar = findViewById(R.id.btnEnviar);
      btnCancelar = findViewById(R.id.btnCancelar);

      rgpSexe = findViewById(R.id.rgpSexe);
      rbtSexe = findViewById(rgpSexe.getCheckedRadioButtonId());
      btnEnviar.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      Intent intencio = Activitat1.this.getIntent();
      intencio.putExtra(nom, etNom.getText().toString());
      intencio.putExtra(cognom, etCognom.getText().toString());
      intencio.putExtra(sexe,rbtSexe.getText().toString());
      startActivity(intencio);
      }
      });

      btnCancelar.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      setResult(RESULT_CANCELED);
      finish();
      }

      });
      }
      }









      share|improve this question















      I am getting a problem with my project in Android Studio.
      What I am trying to do is when I click a button (what opens another activity) and in that activity I write data from a person.
      Then I save this data and the app get back to the main. The next button what does is create a Toast that shows data from the person, but the Toast is returning me null. I don't know what I am doing wrong.



         public class PrincipalActivity extends AppCompatActivity implements View.OnClickListener {

      Button btnObreActivitat1, btnToastDadesPersona;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_principal);

      btnObreActivitat1 = findViewById(R.id.btnObreActivitat1);
      btnObreActivitat1.setOnClickListener(this);
      btnToastDadesPersona = findViewById(R.id.btnToastDadesPersona);
      btnToastDadesPersona.setOnClickListener(this);
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      if (resultCode == RESULT_CANCELED) {
      Toast.makeText(this, "Resposta invalida", Toast.LENGTH_SHORT);
      } else {
      switch (requestCode) {
      case 5:
      setTitle(data.getStringExtra("RESPOSTA"));
      break;
      }
      }
      }

      @Override
      public void onClick(View v) {
      Intent intencio;
      StringBuilder sbUri = new StringBuilder();

      switch (v.getId()){
      case R.id.btnObreActivitat1:
      intencio = new Intent(this,Activitat1.class);
      startActivityForResult(intencio,5);
      break;
      case R.id.btnToastDadesPersona:
      intencio = getIntent();
      String nom = intencio.getStringExtra("NOM");
      String cognom = intencio.getStringExtra("COGNOM");
      String sexe = intencio.getStringExtra("SEXE");

      Toast torrada = new Toast(this);
      LayoutInflater inflador = getLayoutInflater();
      View vista = inflador.inflate(R.layout.torrada,null);
      TextView tvMissatge = vista.findViewById(R.id.tvMissatge);
      tvMissatge.setText("Nom: " + nom + "nCognom: " + cognom + "nSexe: " + sexe);

      torrada.setView(vista);
      torrada.setDuration(Toast.LENGTH_SHORT);
      torrada.show();
      break;
      }
      }

      @Override
      protected void onStart() {
      super.onStart();
      }
      @Override
      protected void onResume(){
      super.onResume();
      }
      @Override
      protected void onPause() {
      super.onPause();
      }
      @Override
      protected void onStop() {
      super.onStop();
      }
      }


      //Next activity where I write data :



       public class Activitat1 extends AppCompatActivity {
      final static String nom = "NOM";
      final static String cognom = "COGNOM";
      final static String sexe = "SEXE";

      Button btnEnviar, btnCancelar;
      EditText etNom, etCognom;
      RadioButton rbtSexe;
      RadioGroup rgpSexe;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_activitat1);

      etNom = findViewById(R.id.etNom);
      etCognom = findViewById(R.id.etCognom);
      btnEnviar = findViewById(R.id.btnEnviar);
      btnCancelar = findViewById(R.id.btnCancelar);

      rgpSexe = findViewById(R.id.rgpSexe);
      rbtSexe = findViewById(rgpSexe.getCheckedRadioButtonId());
      btnEnviar.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      Intent intencio = Activitat1.this.getIntent();
      intencio.putExtra(nom, etNom.getText().toString());
      intencio.putExtra(cognom, etCognom.getText().toString());
      intencio.putExtra(sexe,rbtSexe.getText().toString());
      startActivity(intencio);
      }
      });

      btnCancelar.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      setResult(RESULT_CANCELED);
      finish();
      }

      });
      }
      }






      android android-studio android-intent toast android-toast






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 20:02









      Ali Khaki

      7111418




      7111418










      asked Nov 19 at 16:00









      Jeyem

      33




      33
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
          https://stackoverflow.com/a/947560/3342108






          share|improve this answer




























            up vote
            0
            down vote













            in btnEnviar.setOnClickListener() you are creating a new instance startActivity(intencio); instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();.



            try this



             btnEnviar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent intencio = Activitat1.this.getIntent();
            intencio.putExtra(nom, etNom.getText().toString());
            intencio.putExtra(cognom, etCognom.getText().toString());
            intencio.putExtra(sexe,rbtSexe.getText().toString());
            setResult(RESULT_OK, intencio);
            finish(); }
            });





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378427%2fim-not-getting-data-from-another-activity%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
              https://stackoverflow.com/a/947560/3342108






              share|improve this answer

























                up vote
                0
                down vote



                accepted










                To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
                https://stackoverflow.com/a/947560/3342108






                share|improve this answer























                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
                  https://stackoverflow.com/a/947560/3342108






                  share|improve this answer












                  To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
                  https://stackoverflow.com/a/947560/3342108







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 16:18









                  Natan

                  483




                  483
























                      up vote
                      0
                      down vote













                      in btnEnviar.setOnClickListener() you are creating a new instance startActivity(intencio); instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();.



                      try this



                       btnEnviar.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                      Intent intencio = Activitat1.this.getIntent();
                      intencio.putExtra(nom, etNom.getText().toString());
                      intencio.putExtra(cognom, etCognom.getText().toString());
                      intencio.putExtra(sexe,rbtSexe.getText().toString());
                      setResult(RESULT_OK, intencio);
                      finish(); }
                      });





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        in btnEnviar.setOnClickListener() you are creating a new instance startActivity(intencio); instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();.



                        try this



                         btnEnviar.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        Intent intencio = Activitat1.this.getIntent();
                        intencio.putExtra(nom, etNom.getText().toString());
                        intencio.putExtra(cognom, etCognom.getText().toString());
                        intencio.putExtra(sexe,rbtSexe.getText().toString());
                        setResult(RESULT_OK, intencio);
                        finish(); }
                        });





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          in btnEnviar.setOnClickListener() you are creating a new instance startActivity(intencio); instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();.



                          try this



                           btnEnviar.setOnClickListener(new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                          Intent intencio = Activitat1.this.getIntent();
                          intencio.putExtra(nom, etNom.getText().toString());
                          intencio.putExtra(cognom, etCognom.getText().toString());
                          intencio.putExtra(sexe,rbtSexe.getText().toString());
                          setResult(RESULT_OK, intencio);
                          finish(); }
                          });





                          share|improve this answer












                          in btnEnviar.setOnClickListener() you are creating a new instance startActivity(intencio); instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();.



                          try this



                           btnEnviar.setOnClickListener(new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                          Intent intencio = Activitat1.this.getIntent();
                          intencio.putExtra(nom, etNom.getText().toString());
                          intencio.putExtra(cognom, etCognom.getText().toString());
                          intencio.putExtra(sexe,rbtSexe.getText().toString());
                          setResult(RESULT_OK, intencio);
                          finish(); }
                          });






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 at 16:18









                          k3b

                          10.8k54169




                          10.8k54169






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378427%2fim-not-getting-data-from-another-activity%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Wiesbaden

                              Marschland

                              Dieringhausen