在具有多个活动的Android应用程序中处理活动结果

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..

Thanks

#0

You change the requestCode that you use when you call startActivityForResult.

EDIT: for example, I use this:

startActivityForResult(i, App.REQUEST_ENABLE_BT);

and this:

startActivityForResult(i, App.MANUAL_INPUT);

and then you filter the results like this:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK){
            switch(requestCode){
            case App.REQUEST_ENABLE_BT:
                if(resultCode != RESULT_OK){
                    Toast.makeText(this, getString(R.string.label_bluetooth_disabled), Toast.LENGTH_LONG).show();
                }
                break;
            case App.MANUAL_INPUT:
                break;
        }
}

#1

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;
}

Where ACTIVITY1 and ACTIVITY2 are constants in your Activity. You'd call them like so:

startActivityForResult(activity1Intent, ACTIVITY1);

and

startActivityForResult(activity2Intent, ACTIVITY2);

#2

It's possible to return any kind of data from a subactivity in the result intent parameter:

Sub-activity:

Intent intent = new Intent ();
intent.putExtra ("string_1", "hello");
intent.putExtra ("string_2", "world");
intent.putExtra ("int_1", 1000);
intent.putExtra ("long_1", 2000l);
activity.setResult (Activity.RESULT_OK, intent);

_

Parent activity:

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent intent)
{
    if (resultCode == Activity.RESULT_OK)
    {
        String string_1 = intent.getStringExtra ("string_1", "");
        String string_2 = intent.getStringExtra ("string_2", "");
        int int_1 = intent.getIntExtra ("int_1", 0);
        long long_1 = intent.getLongExtra ("long_1", 0);
    }
}

#3

You can use swicth the requestcode for different result

public void onActivityResult(int requestCode, int resultCode, Intent data)
 {

      super.onActivityResult(requestCode, resultCode, data);

       switch (requestCode) {

          case (1): 
          {
            // do this if request code is 1.
          }
          break;

          case (2):
          {
            // do this if request code is 2.
          }
          break;
  }

推荐文章

是否有方法查找在中定义了反射字段的实际类,或查找子对象的公共基类?

是否有方法查找在中定义了反射字段的实际类,或查找子对象的公共基类?

推荐文章

如何在公开某个特定操作的同时保留其他所有内容的私密性

如何在公开某个特定操作的同时保留其他所有内容的私密性

推荐文章

使用html()编辑标题标记的jQuery问题

使用html()编辑标题标记的jQuery问题

推荐文章

以编程方式将XSN上载到SharePoint

以编程方式将XSN上载到SharePoint

推荐文章

将实体移动到外部jar包后未映射它们

将实体移动到外部jar包后未映射它们

推荐文章

Grails对同一事务的数据库结果分页?

Grails对同一事务的数据库结果分页?

推荐文章

提交的队列revprop

提交的队列revprop

推荐文章

如何引用UIVIiw中的方法?

如何引用UIVIiw中的方法?

推荐文章

在ASP.NET中使用SessionID+MemoryCache是安全和良好的设计吗?

在ASP.NET中使用SessionID+MemoryCache是安全和良好的设计吗?

推荐文章

博客删除操作错误?

博客删除操作错误?

推荐文章

Coldfusion:冲突的cfdump语句

Coldfusion:冲突的cfdump语句

推荐文章

添加原始资源时出错

添加原始资源时出错

推荐文章

同一语言的两个不同区域/域的定位

同一语言的两个不同区域/域的定位

推荐文章

System.IO.FileNotFoundException:无法加载文件或程序集

System.IO.FileNotFoundException:无法加载文件或程序集

推荐文章

如何通过GetPageAsync和XpsDocumentWriter异步使用DocumentPaginator?

如何通过GetPageAsync和XpsDocumentWriter异步使用DocumentPaginator?

推荐文章

请帮助提供扩展WPF内置类的最佳方法

请帮助提供扩展WPF内置类的最佳方法