的onActivityResult不会被调用 [英] OnActivityResult is never called

查看:366
本文介绍了的onActivityResult不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个独立的应用程序。应用程序A和B.应用我要开始从应用程序中的应用程序B的活性,并得到返回结果。在应用程序B还有一个活动。从B秒acitivty我得到结果B的第一项活动。现在,我希望这些结果返回给应用程序A.但在的onActivityResult一个永远不会被调用。以下是code。

应用答:

 公共无效onClickBtnToApplicationB(视图v){
        尝试{
            最终意向意图=新意图(Intent.ACTION_MAIN,NULL);
            最终的组件名CN =新的组件名(pakacagename,package.class);
            intent.setComponent(CN);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivityForResult(意向,REQUEST_ code);
        }赶上(ActivityNotFoundException E){
        //处理异常
        }
    }    公共无效的onActivityResult(INT申请code,INT结果code,意图意图){
        开关(要求code){
            案例REQUEST_ code:
               handleResult(结果code,意向);
               打破;
        }
    }    公共无效handleResult(INT结果code,意图int​​entResult){
        开关(结果code){
            案例RESULT_OK:
                字符串结果= intentResult.getStringExtra(结果);
                //我需要从应用程序B在这里结果..
                打破;             案例RESULT_CANCELED:
                打破;
        }
      }

应用B活动1.class:

 意图S =新意图(1,本,2.class);
startActivityForResult(S,REQUEST_ code_B);
保护无效的onActivityResult(INT申请code,INT结果code,意图int​​entResult){
    开关(要求code){
        案例REQUEST_ code_B:
            handleResult(结果code,intentResult);
    }
}公共无效handleResult(INT结果code,意图int​​entResult){
    开关(结果code){
    案例RESULT_OK:
        字符串scanResult = intentResult.getStringExtra(结果);
        意图newintent =新的Intent();
        newintent.putExtra(结果,scanResult);
        的setResult(Activity.RESULT_OK,newintent);
        完();
        打破;    案例RESULT_CANCELED:
        打破;
}


解决方案

从<一个href=\"http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29\"相对=nofollow>文档:


  

请注意,此方法只应在意向协议来使用的
  被定义为返回一个结果。在其它协议(如
  ACTION_MAIN或ACTION_VIEW),你可能不是你时,你得到的结果
  期望。例如,如果你正在开展活动使用
  singleTask启动模式,它不会在你的任务运行,因此你会
  立即收到取消的结果。


I have two standalone applications. Application A and Application B. I want to start activity in application B from Application A and get the results back. In application B there is one more activity. From B second acitivty i get result to B's first activity. Now I want these result back to Application A. But OnActivityResult in A is never called. Following is the code.

Application A:

public void onClickBtnToApplicationB(View v) {
        try {
            final Intent intent = new Intent(Intent.ACTION_MAIN, null);
            final ComponentName cn = new ComponentName("pakacagename","package.class");
            intent.setComponent(cn);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                 
            startActivityForResult(intent, REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
        //handle Exception
        } 
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        switch (requestCode) {
            case REQUEST_CODE:
               handleResult(resultCode, intent);
               break;
        }
    }

    public void handleResult(int resultCode, Intent intentResult) {
        switch (resultCode) {
            case RESULT_OK:
                String Result = intentResult.getStringExtra("RESULT");
                // I need Results from Application B here..
                break;

             case RESULT_CANCELED:
                break;
        }
      }

Application B Activity 1.class:

Intent s = new Intent(1.this,2.class);
startActivityForResult(s, REQUEST_CODE_B);
protected void onActivityResult(int requestCode, int resultCode, Intent intentResult) {     
    switch(requestCode){
        case REQUEST_CODE_B:
            handleResult(resultCode, intentResult);
    }
}

public void handleResult(int resultCode, Intent intentResult) {
    switch (resultCode) {
    case RESULT_OK:
        String scanResult = intentResult.getStringExtra("RESULT");
        Intent newintent = new Intent();
        newintent.putExtra("RESULT", scanResult);
        setResult(Activity.RESULT_OK, newintent);
        finish();
        break;

    case RESULT_CANCELED:
        break;
}

解决方案

From the documentation:

Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.

这篇关于的onActivityResult不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆