在有一个以上的活动Android应用程序处理onActivityResult [英] Handling onActivityResult in Android app having more than one activity
问题描述
在我的Android应用程序,我有过意向创建两个其他子activites主要活动。现在,无论是子活动返回结果的主要活动。在我的主要活动, 我怎么处理两个onActivityResult(INT申请code,INT结果code,意图数据),因为它不能有两种方法具有相同的名称在给定的类。希望我的问题是清楚的。
In my android app, I have a main activity which creates two other sub activites through intent. Now, both the sub activity return result to the main activity. In my main activity, how do I handle two "onActivityResult(int requestCode, int resultCode, Intent data)" since it cant have two methods with same name in a given class. Hope my question is clear..
感谢
推荐答案
这就是请求code
是什么。所以,你有这样的设置
That's what the requestCode
is for. So you'd have a setup like this
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
case ACTIVITY1:
if(resultCode == RESULT_OK)
Toast.makeText(getApplicationContext(), "Activity 1 returned OK", Toast.LENGTH_LONG).show();
break;
case ACTIVITY2:
if(resultCode == RESULT_OK)
Toast.makeText(getApplicationContext(), "Activity 2 returned OK", Toast.LENGTH_LONG).show();
break;
}
在哪里活动1和活性2在你的活动常数
。你会打电话给他们,像这样:
Where ACTIVITY1 and ACTIVITY2 are constants in your Activity
. You'd call them like so:
startActivityForResult(activity1Intent,活动1);
和
startActivityForResult(activity2Intent,活性2);
这篇关于在有一个以上的活动Android应用程序处理onActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!