Transfer data from one Activity to Another Activit

2019-01-06 17:34发布

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

8条回答
闹够了就滚
2楼-- · 2019-01-06 18:00

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);

查看更多
小情绪 Triste *
3楼-- · 2019-01-06 18:01

Through the below code we can send the values between activities

use the below code in parent activity

Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);

use the below code in child activity

String s= getIntent().getStringExtra(<StringName>);
查看更多
登录 后发表回答