How to pass data between Activities

Asked

Viewed 3,600 times

1

I have a code that transforms getText in string and would like to pass this string to one other Activity in a simple way, but not an Activity that comes right after this.

My code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    editTextEmail = (EditText) findViewById(R.id.email);
    editTextPassword = (EditText) findViewById(R.id.password);
    buttonSignIn = (Button) findViewById(R.id.email_sign_in_button);
    cadastro_button  = (Button) findViewById(R.id.cadastro_button);


    progressDialog = new ProgressDialog(this);

    buttonSignIn.setOnClickListener(this);
    cadastro_button.setOnClickListener(this);


    buttonSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = editTextEmail.getText().toString();
            String password = editTextPassword.getText().toString();

            attemptLogin(email, password);

        }
    });
}

1 answer

5

You should use the Intent for this, follow example:

1ª Activity:

Intent i = new Intent(Activity.this, NewActivity.class);
i.putExtra("key", value);
startActivity(i);

2ª Activity:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("key");
}
  • 1

    Simple and fast ! Thank you very much !

  • @Samaracarvalho If the answer helped you you can mark it as correct using the V on its left side.

  • Leonardo, I tried to change the title of the question, but I’m not sure that’s it (since I know little about Android). Can you review it, please?

  • I changed there jbueno, thank you.

  • I have already given feedback that the answer was useful, but is not visible on account of my reputation..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.