简体   繁体   English

如何通过意图将数据从当前活动发送到新活动?

[英]How to send data to new activity from current activity via intent?

How I can use the intent to send the data to the second class? 如何使用意图将数据发送给第二类?

I am doing this actually 我实际上是在这样做

Intent connectGetData = new Intent(Main.this, GetData.class);
startActivityForResult(connectGetData, REQUEST_GET_DATA); 

From Main.java activity I am calling the Getdata.java activity and I when come back I again get data using. Main.java活动中,我正在调用Getdata.java活动,当我回来时,我再次使用了数据。

public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent) 

Please tell me how I can send data to other activity 请告诉我如何将数据发送到其他活动

You pass data along with the Intent using two mechanisms, depending on the needs of your application. 您可以使用两种机制将数据与Intent一起传递,具体取决于应用程序的需求。

  1. The data field, via Intent.setData() : This is a URI that you can use to indicate the location of a resource the new Activity may need to use 数据字段,通过Intent.setData() :这是一个URI,可用于指示新Activity可能需要使用的资源的位置
  2. Extras, via Intent.putExtra() : You can attach as many extras to an Intent as you like to represent the data you need to pass (both forwards to the new Activity and backwards with the result). 通过Intent.putExtra()附加功能:您可以将任意多个附加附加到Intent,以表示您需要传递的数据(既向前传递到新的Activity,又向后传递结果)。 Extras can be any primitive or easily serializable object. Extras可以是任何原始或易于序列化的对象。

HTH 高温超导

At sending activity... 正在发送活动...

Intent intent = new Intent(current.this, next.class);
intent.putextra("keyName","value");
startActivity(intent);

At receiving activity... 在接受活动时...

String data = getIntent().getExtras().getString("keyName");

Thus you can have data at receiving activity from sending activity... 因此,您可以从发送活动的接收活动中获得数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过意图将数据从活动发送到服务-空指针问题 - How to send data via intent from an Activity to Service - Null Pointer Issue 启动新活动后,先前的活动是否有可能继续通过意图将数据发送到新活动? - Is it possible that after launching new activity the previous activity keeps sending data to new activity via intent? 如何设置一个意图,以便我可以将数据发送到第二个活动,然后从那里发送到第三个活动? - How to set an intent so that I can send a data to the second activity and from there to the third activity? 如何从新的活动意图中获取视图 - How to get a view from new activity intent MediaPlayer并通过Intent返回当前活动 - MediaPlayer and returning to current activity via Intent 如何将当前用户的特定数据从 firebase 显示到新活动? - How to display specific data of current user from firebase to new activity? 如何从 Activity 获取 Intent 数据到 Fragment - How to get Intent Data from Activity into Fragment 如何将意图数据设置为当前活动editText? - How do I set intent data to current activity editText? 如何将数据发送到新的 Android 活动 - How to send data to new Android activity 将带有额外字符串的意图从活动发送到选项卡片段 - send intent with extrastring from activity to tab fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM