Search code examples
androidandroid-intentbundle

On Android, the variable is not being passed from one class to another class via Intent


In this simple code that you can copy and paste into Android Studio to test, there are two classes. In the first class, which is called MainActivity, there is a field for a user to type the name. In the second class called MainActivity2, there is another field to show what the user typed in the first class called MainActivity1. However, this code is showing error that it does not recognize the variable "name" in the MainActivity2, in this code snippet: String receivedName = receiverBundle.getString("name");

Please can anyone find out how to resolve this little error?

Please, you can copy and paste these simples codes and test in your Android Studio:

/*This code is the first Java class that there is a field where the user can write own name:*/
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;

MainActivity1:

``` public class MainActivity extends AppCompatActivity {
    EditText name;
    Button button;

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

      name  = findViewById(R.id.name);
      button = findViewById(R.id.button);

        String nameInString = name.getText().toString();

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent senderIntent = new Intent(getApplicationContext(), MainActivity2.class);
                Bundle senderBundle = new Bundle();
                senderBundle .putString("name", nameInString);
                startActivity(senderIntent);
            }
        });

    }
} 
/* This class there is the field that must appear the name that the user wrote on the first class */
```
MainActivity2:
``` 
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

    TextView resultName;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        resultName = findViewById(R.id.nameReceived);

        Intent receiverIntent = getIntent();
        Bundle receiverBundle = receiverIntent.getExtras();
        String receivedName = receiverBundle.getString
                ("name");

        resultName.setText(receivedName);

    }
}
/* Front-end code of MainActivity1*/
``` 
activity_main.xml
``` 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.645" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Complete Name:"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.372"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.169" />

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Type your name"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.501" />

</androidx.constraintlayout.widget.ConstraintLayout>

/* Front-end code of MainActivity2*/ Activity_main2.xml

    <TextView
        android:id="@+id/nameReceived"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Received name:"
        android:textSize="25dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.476"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.307" />

</androidx.constraintlayout.widget.ConstraintLayout>``` 

enter image description here


Solution

  • The init of nameInString should be happen in button.setOnClickListener!

        button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View view) {
                String nameInString = name.getText().toString();
                Intent senderIntent = new Intent(getApplicationContext(), MainActivity2.class);
                Bundle senderBundle = new Bundle();
                senderIntent.putExtra("name",nameInString);
                startActivity(senderIntent);
            }
        });
    

    MainActivity2.class

            Intent intent = getIntent();
            String username= intent.getStringExtra("name");