Transfer data from one Activity to Another Activity Using Intents

后端 未结 8 1596
花落未央
花落未央 2020-11-29 06:36

I would like to be able to transfer data from one activity to another activity. How can this be done?

8条回答
  •  庸人自扰
    2020-11-29 07:25

    You just have to send extras while calling your intent

    like this:

    Intent intent = new Intent(getApplicationContext(), SecondActivity.class); intent.putExtra("Variable Name","Value you want to pass"); startActivity(intent);

    Now on the OnCreate method of your SecondActivity you can fetch the extras like this

    If the value u sent was in "long"

    long value = getIntent().getLongExtra("Variable Name which you sent as an extra", defaultValue(you can give it anything));

    If the value u sent was a "String"

    String value = getIntent().getStringExtra("Variable Name which you sent as an extra");

    If the value u sent was a "Boolean"

    Boolean value = getIntent().getStringExtra("Variable Name which you sent as an extra",defaultValue);

提交回复
热议问题