Home > OS >  Android Studio My name is not displaying in second activity after passing through first Activity
Android Studio My name is not displaying in second activity after passing through first Activity

Time:09-14

Main Activity 2

MAIN ACTIVITY

Android Studio My name is not displaying in second activity after passing through first Activity

CodePudding user response:

In your Main Activity: Please call intent.putExtra(), before startActivity(). Example:

val intent = Intent(this, NewActivity::class.java) 
intent.putExtra("username","Name")
startActivity(intent)

And fetch like that (in Main Activity 2)

val bundle :Bundle?=intent.extras 
if (bundle!=null){ 
val user_name = bundle.getString("username") 
}

CodePudding user response:

I noticed that in the 'MainActivity.java' you are starting new activity before passing the string into the intent, just switch the both lines 30 and 31.

CodePudding user response:

In your MainActivity, you should putExtra before startActivity. Like this:

    Intent intent = new Intent(...);
    intent.putExtra...;
    startActivity(intent);

CodePudding user response:

In your MainActivity, create a new Intent:

String name="abcdef;
Intent i = new Intent(MainActivity.this, MainActivity2.class);    
i.putExtra("key",name);
startActivity(i);

Then in the MainActivity2, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String name = extras.getString("key");
    //"key" must match that used in the other activity
}
  • Related