如何将数据从一项活动(意图)发送到另一项活动?
我使用此代码发送数据:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
user.getUserAccountId()+""
中将 int 转换为字符串的一种很好、快速的方法,因为这样会创建要收集的不必要的对象。考虑改用 String.valueOf(user.getUserAccountId)
或 Integer.toString(user.getUserAccountId)
。
首先,使用 getIntent()
方法获取启动您的活动的意图:
Intent intent = getIntent();
如果您的额外数据表示为字符串,那么您可以使用 intent.getStringExtra(String name)
方法。在你的情况下:
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");
在接收活动中
Bundle extras = getIntent().getExtras();
String userName;
if (extras != null) {
userName = extras.getString("name");
// and get whatever type user account id is
}
getStringExtra?
更可取
null
,则可以跳过整个 extras 提取。通过使用 getStringExtra
,您基本上可以将其更改为一系列 if(extras != null) { return extras.getString(name) }
。您调用的每个 getStringExtra
对应一个。此选项将检查一次 null
,如果是,则根本不会阅读 Bundle
。除此之外,getStringExtra
也可能每次都在内部继续询问 getExtras
。因此,您只需对函数进行更多调用。
// 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
加起来
设置数据
String value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);
获取数据
String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("sample_name");
}
无需初始化另一个新的 Intent 来接收数据,只需执行以下操作:
String id = getIntent().getStringExtra("id");
按意图放置数据:
Intent intent = new Intent(mContext, HomeWorkReportActivity.class);
intent.putExtra("subjectName", "Maths");
intent.putExtra("instituteId", 22);
mContext.startActivity(intent);
按意图获取数据:
String subName = getIntent().getStringExtra("subjectName");
int insId = getIntent().getIntExtra("instituteId", 0);
如果我们为意图使用整数值,我们必须在 getIntent().getIntExtra("instituteId", 0)
中将第二个参数设置为 0。否则,我们不使用 0,Android给我一个错误。
如果在 FragmentActivity 中使用,请尝试以下操作:
第一个页面扩展了 FragmentActivity
Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);
在片段中,您只需要先调用 getActivity()
,
第二页扩展了 Fragment:
String receive = getActivity().getIntent().getExtras().getString("name");
如果您试图在片段中获取额外数据,那么您可以尝试使用:
使用以下方式放置数据:
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);
使用以下方式获取数据:
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);
}
科特林
第一个活动
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)
第二次活动
val value = getIntent().getStringExtra("key")
建议
始终将密钥放在常量文件中以获得更多管理方式。
companion object {
val PUT_EXTRA_USER = "PUT_EXTRA_USER"
}
您可以从意图中获取任何类型的额外数据,无论它是对象、字符串还是任何类型的数据。
Bundle extra = getIntent().getExtras();
if (extra != null){
String str1 = (String) extra.get("obj"); // get a object
String str2 = extra.getString("string"); //get a string
}
最短的解决方案是:
Boolean isGranted = getIntent().getBooleanExtra("tag", false);
只是一个建议:
我建议不要在 i.putExtra("id".....) 中使用“id”或“name”,而是在有意义时使用可与 putExtra() 一起使用的当前标准字段,即Intent.EXTRA_something。
完整列表可在 Intent(Android 开发者)中找到。
我们可以通过简单的方式做到这一点:
在第一个活动中:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);
在第二活动中:
try {
Intent intent = getIntent();
String uid = intent.getStringExtra("uid");
String pwd = intent.getStringExtra("pwd");
} catch (Exception e) {
e.printStackTrace();
Log.e("getStringExtra_EX", e + "");
}
在 First Activity 上传递带有值的意图:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);
接收第二个活动的意图;-
Intent intent = getIntent();
String user = intent.getStringExtra("uid");
String pass = intent.getStringExtra("pwd");
我们通常使用两种方法来发送值和获取值。为了发送值,我们将使用 intent.putExtra("key", Value);
并且在接收另一个活动的意图期间,我们将使用 intent.getStringExtra("key");
来获取意图数据作为 String
或使用其他可用方法来获取其他类型的数据(Integer
、{ 5}等)。关键可以是任何关键字来识别价值意味着你正在分享什么价值。希望它对你有用。
你也可以这样做 // 把价值放在意图中
Intent in = new Intent(MainActivity.this, Booked.class);
in.putExtra("filter", "Booked");
startActivity(in);
// 从意图中获取值
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String filter = bundle.getString("filter");
从 Intent 中获取不同类型的 Extra
要从 Intent 访问数据,您应该知道两件事。
钥匙
数据的数据类型。
Intent 类中有不同的方法来提取不同类型的数据类型。看起来像这样
getIntent().XXXX(KEY) 或 intent.XXX(KEY);
因此,如果您知道在 otherActivity 中设置的变量的数据类型,则可以使用相应的方法。
从 Intent 中检索 Activity 中的字符串的示例
String profileName = getIntent().getStringExtra("SomeKey");
不同数据类型的不同方法变体列表
您可以在 Intent 的官方文档中查看可用方法的列表。
这适用于适配器,对于活动,您只需将 mContext 更改为您的 Activty 名称,对于片段,您需要将 mContext 更改为 getActivity()
public static ArrayList<String> tags_array ;// static array list if you want to pass array data
public void sendDataBundle(){
tags_array = new ArrayList();
tags_array.add("hashtag");//few array data
tags_array.add("selling");
tags_array.add("cityname");
tags_array.add("more");
tags_array.add("mobile");
tags_array.add("android");
tags_array.add("dress");
Intent su = new Intent(mContext, ViewItemActivity.class);
Bundle bun1 = new Bundle();
bun1.putString("product_title","My Product Titile");
bun1.putString("product_description", "My Product Discription");
bun1.putString("category", "Product Category");
bun1.putStringArrayList("hashtag", tags_array);//to pass array list
su.putExtras(bun1);
mContext.startActivity(su);
}
不定期副业成功案例分享
getIntent().getStringExtra("id");
以获取 id 字符串getIntent()
方法获取启动活动的意图。我已经更新了答案。