skip to Main Content

Main Activity 2

MAIN ACTIVITY

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

4

Answers


  1. 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") 
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. In your MainActivity, you should putExtra before startActivity. Like this:

        Intent intent = new Intent(...);
        intent.putExtra...;
        startActivity(intent);
    
    Login or Signup to reply.
  4. 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
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search