将参数传递给意图 android [英] Passing arguments to intent android
本文介绍了将参数传递给意图 android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有第一个意图,它开始了第二个意图.在第二个意图中,我获取值,并将值传递给第一个内容并关闭第二个内容.我该怎么做?
I have the first intent, it starts the second intent. In the second intent, I get the values, and pass the value to the first content and close the second content. How can I do it?
推荐答案
您可以在创建意图时直接将参数传递给它.如果需要传递对象,则需要在传递的对象上实现 Parcelable 接口:
You can directly pass parameters to the intent when you create it. If you need to pass objects you need to implement Parcelable interface on the object you pass:
Intent i = new Intent(MyActivity.this, SecondActivity.class);
MyData j = new MyData();
i.putExtra("MyParameter", "Something");
i.putExtra("MyData", j); //only works if MyData implements Parcelable
startActivity(i);
在第二个活动中,您可以读取数据:
In the second activity you can read your data:
Intent i = getIntent();
Bundle extras = i.getExtras();
if(extras.containsKey("MyParameter")) {
String something = i.getStringExtra("MyParameter");
}
if(extras.containsKey("MyData")) {
MyData otherthing = i.getParcelableExtra("MyData");
}
希望能帮到你
这篇关于将参数传递给意图 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文