0

I've been following a class, but it didn't seems to work (or maybe I'm too bad and got something wrong).

I should input a name in ContenidoActivity.java, press a button (onClick: enviarNombre is selected) and get the name to the DestinoActivity.java txt, but it just stays there, it didn't change anything when I Click the button. I have put a toast "has seleccionado enviar" which works when the button is clicked, but nothing else works as far as I can see.

ContenidoActivity.java:

package com.example.crehana2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class ContentActivity extends AppCompatActivity {
    public static final String NOMBRE_USUARIO="Paris";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);

    }

    public void enviarNombre(View view){
        Toast.makeText(getApplicationContext(),"Has seleccionado enviar", Toast.LENGTH_LONG).show();

        Intent intentEnviar = new Intent(this, DestinoActivity.class);
        EditText nombreUsuario = findViewById(R.id.txtNombre);
        String nombreUSuarioMensaje = nombreUsuario.getText().toString();

        intentEnviar.putExtra(NOMBRE_USUARIO, nombreUSuarioMensaje);

    }


}

DestinoActivity.java:

package com.example.crehana2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class DestinoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_destino);
        
        Intent intentFinal = getIntent();
        String nombreUsuarioFinal=intentFinal.getStringExtra(ContentActivity.NOMBRE_USUARIO);
        
        TextView mensajeFinal = findViewById(R.id.mensajefinal);
        mensajeFinal.setText(nombreUsuarioFinal);
    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • android studio is an IDE - that's what you're writing your code in. if you're not asking about a feature of the IDE specifically, it really doesn't make any difference to your question. if you're asking about `android` then you should tag your question as `android` and not just `android-studio` because you're using it. i've updated your tags, remember this for future because it has a big impact on your question – a_local_nobody Mar 14 '21 at 14:26
  • Thanks for your comment, it didn't help solving the problem, but thanks anyway – Henry Aranda Mar 14 '21 at 14:27
  • `it didn't help solving the problem` if you leave your question with the wrong tags then people wont see it, so it _does_ help in solving it. – a_local_nobody Mar 14 '21 at 14:28
  • Does this answer your question? [How to start new activity on button click](https://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – a_local_nobody Mar 14 '21 at 14:35

1 Answers1

1
public void enviarNombre(View view){
    Toast.makeText(getApplicationContext(),"Has seleccionado enviar", Toast.LENGTH_LONG).show();

    Intent intentEnviar = new Intent(this, DestinoActivity.class);
    ...
    intentEnviar.putExtra(NOMBRE_USUARIO, nombreUSuarioMensaje);

}

What have you done here ?

Well, you've created an intent, gave it some values and then... done nothing with it.

You need to actually schedule the intent:

startActivity(intentEnviar);
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • That's the same thing I though, but in the tutorial seems that selecting "enviarNombre" in the interface of android studio where it says "onClick", would work fine. And the "Toast" it's send, but not the other part. the "startActivity" is not in the tutorial, but i'll try it just to be sure – Henry Aranda Mar 14 '21 at 14:36
  • with all due respect, i don't have any reference to your tutorial so that's a bit irrelevant. the toast _should_ show because you've told it to `Toast.makeText(getApplicationContext(),"Has seleccionado enviar", Toast.LENGTH_LONG).show();` <-- you're calling .show() here which will schedule it – a_local_nobody Mar 14 '21 at 14:38
  • Thank it worked out. I'll add this link to the tutorial if you don't mind, so people won't get behind in the same problem. also I did put it like this: - startActivity(intentEnviar.putExtra(NOMBRE_USUARIO, nombreUSuarioMensaje)); - pd: Thank you, I didn't knew that last fact – Henry Aranda Mar 14 '21 at 14:40
  • add it to your original post, maybe someone in future has the same problem – a_local_nobody Mar 14 '21 at 14:40