Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Can I use putExtra to pass multiple values

People also ask

How can I pass multiple EditText values to another activity in Android?

You need put them in Extras (putExtras) and then pass from the current activity to the other one. You need capture your EditText value as String and then putExtra with Key - one each for your need and then retrieve them in the second activity.

What is the work of putExtra () method?

A quick overview of the internals is that Intents use Android's IPC (Inter-process communication). Essentially, the only data types that are OS-friendly are primitive types (int, long, float, boolean, etc...), this is why putExtra() allows you to store primitives only.

What is the use of putExtra in Android?

putExtra() adds extended data to the intent. It has two parameters, first one specifies the name which of the extra data,and the second parameter is the data itself.


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! :-)


You can pass multiple values by using multiple keys. Instead of

i.putExtra(ID_EXTRA, "1", "111");

do

i.putExtra(ID_EXTRA1, "1");
i.putExtra(ID_EXTRA2, "111");

Of course you have to define 2 constants for the keys and have to read both seperately in the new activity.

Or you can pass a string array via

i.putExtra(ID_EXTRA, new String[] { "1", "111"});

Putting extra values in class

public class MainActivity extends Activity {
        public final static String USERNAME = "com.example.myfirstapp.MESSAGE";
        public final static String EMAIL = "com.example.myfirstapp.EMAIL";

public void registerUser(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText userNameTxt = (EditText) findViewById(R.id.editText1);
        EditText emailTxt = (EditText) findViewById(R.id.editText2);
        String userName = userNameTxt.getText().toString();
        String email = emailTxt.getText().toString();
        intent.putExtra(USERNAME, userName);
        intent.putExtra(EMAIL,email);
        startActivity(intent);

    }

Reading extra values from another class

public class DisplayMessageActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String user = intent.getStringExtra(MainActivity.USERNAME);
        String email = intent.getStringExtra(MainActivity.EMAIL);   

No you can't but you can pass an array using:

public Intent putExtra (String name, String[] value)

like this for example:

i.putExtra(ID_EXTRA, new String[]{"1", "111"});