Adh*_*ham 719 android android-intent
如何将数据从一个活动(意图)发送到另一个活动?
我用这段代码发送数据:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
Run Code Online (Sandbox Code Playgroud)
Mal*_*olm 1158
首先,使用该getIntent()方法获取已启动活动的意图:
Intent intent = getIntent();
Run Code Online (Sandbox Code Playgroud)
如果您的额外数据表示为字符串,则可以使用intent.getStringExtra(String name)方法.在你的情况下:
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");
Run Code Online (Sandbox Code Playgroud)
Nic*_*ckT 185
在接收活动中
Bundle extras = getIntent().getExtras();
String userName;
if (extras != null) {
userName = extras.getString("name");
// and get whatever type user account id is
}
Run Code Online (Sandbox Code Playgroud)
Sum*_*rma 38
// How to send value using intent from one class to another class
// class A(which will send data)
Intent theIntent = new Intent(this, B.class);
theIntent.putExtra("name", john);
startActivity(theIntent);
// How to get these values in another class
// Class B
Intent i= getIntent();
i.getStringExtra("name");
// if you log here i than you will get the value of i i.e. john
Run Code Online (Sandbox Code Playgroud)
iti*_*skj 29
String value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("sample_name");
}
Run Code Online (Sandbox Code Playgroud)
r_a*_*ela 17
不要初始化另一个新的Intent来接收数据,而是这样做:
String id = getIntent().getStringExtra("id");
Run Code Online (Sandbox Code Playgroud)
小智 11
如果在FragmentActivity中使用,请尝试以下方法:
第一页扩展了FragmentActivity
Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);
Run Code Online (Sandbox Code Playgroud)
在片段中,您只需getActivity()要先调用,
第二页扩展片段:
String receive = getActivity().getIntent().getExtras().getString("name");
Run Code Online (Sandbox Code Playgroud)
如果您尝试在片段中获取额外数据,那么您可以尝试使用:
放置数据使用:
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);
Run Code Online (Sandbox Code Playgroud)
获取数据使用:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getArguments().getInt(ARG_SECTION_NUMBER);
getArguments().getString(ARG_SECTION_STRING);
getArguments().getBoolean(ARG_SECTION_BOOL);
getArguments().getChar(ARG_SECTION_CHAR);
getArguments().getByte(ARG_SECTION_DATA);
}
Run Code Online (Sandbox Code Playgroud)
第一个活动
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)
Run Code Online (Sandbox Code Playgroud)
第二项活动
val value = getIntent().getStringExtra("key")
Run Code Online (Sandbox Code Playgroud)
建议
始终将密钥放在常量文件中以获得更易于管理的方式。
companion object {
val PUT_EXTRA_USER = "PUT_EXTRA_USER"
}
Run Code Online (Sandbox Code Playgroud)
您可以从 intent 获取任何类型的额外数据,无论它是对象、字符串还是任何类型的数据。
Bundle extra = getIntent().getExtras();
if (extra != null){
String str1 = (String) extra.get("obj"); // get a object
String str2 = extra.getString("string"); //get a string
}
Run Code Online (Sandbox Code Playgroud)
和最短溶液是:
Boolean isGranted = getIntent().getBooleanExtra("tag", false);
Run Code Online (Sandbox Code Playgroud)
小智 6
按意图放置数据:
Intent intent = new Intent(mContext, HomeWorkReportActivity.class);
intent.putExtra("subjectName", "Maths");
intent.putExtra("instituteId", 22);
mContext.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
按意图获取数据:
String subName = getIntent().getStringExtra("subjectName");
int insId = getIntent().getIntExtra("instituteId", 0);
Run Code Online (Sandbox Code Playgroud)
如果我们为意图使用整数值,我们必须在 中将第二个参数设置为 0 getIntent().getIntExtra("instituteId", 0)。否则,我们不使用 0,Android 会给我一个错误。
| 归档时间: |
|
| 查看次数: |
827702 次 |
| 最近记录: |