Android, Can I use putExtra to pass multiple values

前端 未结 6 1574
北恋
北恋 2020-12-02 06:54

I want to pass two values to another activity can I do this with putExtra or do I have to do it a more complicated way, which it seems from my reading. E.g.. can something l

6条回答
  •  萌比男神i
    2020-12-02 07:27

    You could pass a 'bundle' of extras rather than individual extras if you like, for example:-

    Intent intent = new Intent(this, MyActivity.class);
    Bundle extras = new Bundle();
    extras.putString("EXTRA_USERNAME","my_username");
    extras.putString("EXTRA_PASSWORD","my_password");
    intent.putExtras(extras);
    startActivity(intent);
    

    Then in your Activity that your triggering, you can reference these like so:-

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    String username_string = extras.getString("EXTRA_USERNAME");
    String password_string = extras.getString("EXTRA_PASSWORD");
    

    Or (if you prefer):-

    Bundle extras = getIntent().getExtras();
    String username_string = extras.getString("EXTRA_USERNAME");
    String password_string = extras.getString("EXTRA_PASSWORD");
    

    Hope this helps! :-)

提交回复
热议问题