OnActivityResult is never called

天涯浪子 提交于 2019-12-24 20:39:43

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/17976073/onactivityresult-is-never-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!