简体   繁体   English

意图导致活动崩溃

[英]Intent causing Activity to crash

I have an activity that receives .getExtras() from two different activities. 我有一个活动,它从两个不同的活动中接收.getExtras()。 The only problem is that it crashes because of the two different .getExtras() being set on that activity. 唯一的问题是由于在该活动上设置了两个不同的.getExtras()而导致崩溃。 How can I overcome this issue. 我该如何克服这个问题。 Like could I make an activity pass some sort of unique ID to the other Activity. 就像我可以让一个活动将某种唯一ID传递给另一个活动一样。

Thanks 谢谢

check if the extra of key is found then do your code like the following 检查是否找到了额外的密钥,然后像下面这样执行代码

if(getIntent().hasExtra("Name"))
{
         //Write your code here
}

Pass a boolean extra from both activity and make that value true for one activity and false for other activity and when you getextra in receiving activity first get that boolean value and than get all other data base on that Key 从两个活动中传递一个布尔附加值,并使一个活动的值正确,而对其他活动的值则返回false,当您在接收活动时首先获得该布尔值,然后获得该键上的所有其他数据库

Intent actA=new Intent(CurrentAct.this, ActivityA.class);  //Activity A code
actA.putExtra("Key",true);
startActivity(actA);


Intent actB=new Intent(CurrentAct.this, ActivityB.class); //Activity B code
actB.putExtra("Key",true);
startActivity(actB);


Bundle extras = getIntent().getExtras(); //Receiving Activity Code
boolean mKey=extras.getBoolean("Key");
if(mKey){
   //Activity A calling
}else{
 //Activity B calling
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM