startActivityForResult()不适用于外部活动 [英] startActivityForResult() not working for external Activity

查看:202
本文介绍了startActivityForResult()不适用于外部活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是重复的问题.我只发现了这个问题,但它没有没有任何可行的解决方案.

This is not a duplicate question. I only found this question relevant but it doesn't have any working solution.

我有什么

我有两个由我创建的应用程序,分别是App A和AppB.我的要求是,单击按钮即可从App A中打开AppB.现在,应用B会进行一些处理,并向应用A返回一个小状态.不是吗?

I have two apps both created by me, App A and App B. My requirement is to open App B from App A on click of a button. Now, App B will do some processing and return a small status to App A. This should be possible isn't it?

我做了什么

Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(Config._PACKAGE);
getActivity().startActivityForResult(intent, 6699);

这将完美打开AppB.现在在应用B中,

This opens the App B perfectly. Now in App B,

 Intent returnIntent = new Intent();
 returnIntent.putExtra("STATUS", statusCode);
 setResult(RESULT_OK, returnIntent);
 finish();

这是我在Activity上的onActivityResult()方法.我是从片段开始活动的.

And this is my onActivityResult() method on the Activity. I am starting the Activity from a Fragment though.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        Log.d(TAG, "ON ACTIVITY RESULT");

        if (requestCode == 6699) {

            if (resultCode == RESULT_OK) {

                Log.d(TAG, "RESULT OK");

            }
            if (resultCode == RESULT_CANCELED) {

                Log.d(TAG, "RESULT NOT OK");

            }
        }
    }

问题

当我使用RESULT_CANCELLED.启动App B的活动时,就会调用onActivityResult().这怎么可能?是因为这是一项新任务吗?

The onActivityResult() gets called just when I start the activity of App B with RESULT_CANCELLED. How is this possible? Is it because its a new task?

但是当我在App B上调用finish()时,它关闭并带我回到App A,但是这次不调用onActivityResult()吗?我该如何解决?

But when I call finish() on App B and it closes and takes me back to App A, but the onActivityResult() is not called this time? How can I solve this?

这是一个非常基本的要求,应该有一个解决方案.

推荐答案

我终于解决了这个问题.

I finally solved this problem.

如果您要启动的活动使用singleTask启动模式,它将 将不会在您的任务中运行,因此您将立即收到一个 取消结果

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

这就是文档所说的.现在的想法是,如果被调用的活动在新任务上启动,则结果将立即被取消.就我而言,我是完全从另一个应用程序开始的活动,无论是否使用launchModetaskAffinity或标志,该活动始终在新任务中启动.

This is what the docs says. Now the idea is that if the called activity is started on a new task, the result will be cancelled immediately. In my case, I am starting an activity from a different app altogether, which is always started in a new task, regardless of any launchMode or taskAffinity or flag.

每当我这样做时,

Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(Config._PACKAGE);
getActivity().startActivityForResult(intent, 6699);

此标志Intent.FLAG_ACTIVITY_NEW_TASK是默认自动添加的.因此,我们需要在使用开始活动之前清除所有标志,

this flag Intent.FLAG_ACTIVITY_NEW_TASK is automatically added by default. So, we need to clear all flags before starting the activity using,

intent.setFlags(0);

在调用startActivity()之前使用它.解决方案既简单又棘手.

Use it before calling startActivity(). The solution was simple and tricky at the same time.

这篇关于startActivityForResult()不适用于外部活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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