Skip to content
Advertisement

How do I pass ArrayLists to another activity, modify it, and return it back to use?

I’m trying to create a login/register part of a project, and I’m having trouble with passing the sign-up information back to the login activity. I initialized username_info, password_info, and name_info in MainActivity, and I want to send it to SignUpActivity through Intent.

        Intent i = new Intent(this, SignUpActivity.class);
        i.putExtra("username_info", username_info);
        i.putExtra("password_info", password_info);
        i.putExtra("name_info", name_info);
        startActivityForResult(i, 101);

After values are added in the other activity, it’s sent back like this (the arraylists have the same name in both activities):

        Intent r = new Intent();
        r.putExtra("username_info", username_info);
        r.putExtra("password_info", password_info);
        r.putExtra("name_info", name_info);
        setResult(Activity.RESULT_OK, r);
        finish();
    }

and it’s received here:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101) {
            if (resultCode == Activity.RESULT_OK) {
                Bundle e = getIntent().getExtras();
                username_info = e.getStringArrayList("username_info");
                password_info = e.getStringArrayList("password_info");
                name_info = e.getStringArrayList("name_info");
            }
        }
    }

But the array lists are unchanged when I get back to the MainActivity. I’m new to Android Studio, so I might just be making a simple mistake.

EDIT: I’m crashing when the username and password don’t match, but it should be returning a toast instead:

@Override
            public void onClick(View v) {
                username = username_input.getText().toString();
                password = password_input.getText().toString();
                int index = username_info.indexOf(username);

                if (username_info.size() < 1) {
                    Toast.makeText(MainActivity.this, "You must sign up first", Toast.LENGTH_SHORT).show();
                }
                else if (password_info.get(index).equals(password)) {
                    Toast.makeText(MainActivity.this, "make an activity", Toast.LENGTH_SHORT).show();
                        // open activity
                }
                else {
                    Toast.makeText(MainActivity.this, "Incorrect username/password", Toast.LENGTH_SHORT).show();
                }
            }
        });

Advertisement

Answer

Don’t use getIntent() in onActivityResult, instead use the Intent data:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101) {
            if (resultCode == Activity.RESULT_OK) {
                Bundle e = data.getExtras();
                username_info = e.getStringArrayList("username_info");
                password_info = e.getStringArrayList("password_info");
                name_info = e.getStringArrayList("name_info");
            }
        }
    }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement