如何启动一个Intent传递一些参数呢? [英] How to start an Intent by passing some parameters to it?
问题描述
我想通过我的ListActivity的构造函数一些变量
I would like to pass some variables in the constructor of my ListActivity
我通过这个code开始的活动:
I start activity via this code:
startActivity(new Intent (this, viewContacts.class));
我想用类似code,但两个字符串传递给构造函数。怎么可能?
I would like to use similar code, but to pass two strings to the constructor. How is possible?
推荐答案
为了通过创建新的目标参数,把参数映射:
In order to pass the parameters you create new intent and put a parameter map:
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("firstKeyName","FirstKeyValue");
myIntent.putExtra("secondKeyName","SecondKeyValue");
startActivity(myIntent);
为了得到开始活动里面的参数值,你必须调用获取【类型】超()
在同一个目的:
In order to get the parameters values inside the started activity, you must call the get[type]Extra()
on the same intent:
// getIntent() is a method from the started activity
Intent myIntent = getIntent(); // gets the previously created intent
String firstKeyName = myIntent.getStringExtra("firstKeyName"); // will return "FirstKeyValue"
String secondKeyName= myIntent.getStringExtra("secondKeyName"); // will return "SecondKeyValue"
如果你的参数是整数,你会使用 getIntExtra()
代替等。
现在你可以使用你的参数,就像你通常会。
If your parameters are ints you would use getIntExtra()
instead etc.
Now you can use your parameters like you normally would.
这篇关于如何启动一个Intent传递一些参数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!