[英]android Intent getextras from two classes
I have the following method: 我有以下方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView name=(TextView)findViewById(R.id.nom);
Bundle extras = getIntent().getExtras();//from class 1
Bundle extras2=getIntent().getExtras();//from class/
name.setText(extras.getString("nom"));//if methode in class1 works
name.setText(extras2.getString("nombank"));//if methode in class 2 works
}
But i get empty name
. 但是我
name
空name
。
I need to get pass two Strings from two different classes(class1 and class2) to the MainActivity.java
我需要从两个不同的类(class1和class2)传递两个String到
MainActivity.java
Activity cannot be started by two different activities, just by one, so you must not use two different extras in your main class - you can use just one extra and put data with the same key value in both activities. 活动不能由两个不同的活动开始,而不能由一个活动开始,因此您不能在主类中使用两个不同的附加项-您只能使用一个附加项并将两个键值相同的数据放入两个活动中。
So, for example, if you have Class1 and Class2, when you create intent in both classes you should use: 因此,例如,如果您具有Class1和Class2,则在两个类中都创建意图时,应使用:
Intent int = new Intent(this,MainActivity.class);
int.putExtra("nom",your_String);
...//Another code
And in MainActivity
you get just one extra: 在
MainActivity
您只能得到一个额外的MainActivity
:
Bundle extra = getIntent().getExtras();
name.setText(extra.getString("nom"));
If Class1 and Class2 are started from MainActivity
, use startActivityForResult()
method instead. 如果从
MainActivity
启动Class1和Class2,请改用startActivityForResult()
方法。
You don't need any of that. 您不需要任何。 Just do as shown below:
只需如下所示:
name.setText(getIntent().getStringExtra("nom")); name.setText(getIntent().getStringExtra("nombank"));
That's It, This is All You Need. 就这样,这就是您所需要的。
But you can't Put Two Intent Extras in the Same TextView. 但是您不能在同一个TextView中放置两个Intent Extras。 You can Only USE ONE
您只能使用一个
Just pass extra with same key and get in activity 只需使用相同的密钥传递其他内容即可参与活动
Class 1 1类
Intent class1 = new Intent(this,MainActivity.class);
class1.putExtra("same_key","class 1 string");
Class 2 2级
Intent class2 = new Intent(this,MainActivity.class);
class2.putExtra("same_key","class 2 string");
MainActivity 主要活动
Bundle extras = getIntent().getExtras();
//Called from Class1 show "class 1 string"
//Called from Class2 show "class 2 string"
name.setText(extras.getString("same_key"));
If MainActivity will called from Class 1 extra string will be from Class 1, if from Class 2 extra string will be from Class2. 如果从Class 1调用MainActivity,则从Class 1调用额外的字符串;如果从Class 2调用,则从Class2调用额外的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.