简体   繁体   English

Android:startActivityForResult没有调用onActivityResult

[英]Android: startActivityForResult not calling onActivityResult

My Setup 我的设置

  • A Service running in its own process, CentralService 在自己的进程中运行的服务,CentralService
  • An activity that calls startActivityForResult(), MainActivity 一个调用startActivityForResult(),MainActivity的活动
  • The activity that is being started for result, ReturnResultActivity 正在为结果,ReturnResultActivity启动的活动

What I'm trying to do 我想做什么

  1. Start ReturnResultActivity and have it bind to the service (register its handler) 启动ReturnResultActivity并将其绑定到服务(注册其处理程序)
  2. Let whatever other activities want to run run 让任何其他想要运行的活动运行
  3. When it receives a message from the service: 当它收到来自服务的消息时:
    • Unbind from the Service 解除服务绑定
    • finish() 完()
    • setResult() 的setResult()
      1. Have the MainActivity's onActivityResult() method called 调用MainActivity的onActivityResult()方法

Using Log.i I have confirmed that steps 1 to 3 happen. 使用Log.i我已经确认步骤1到3发生了。 However when the onActivityResult() method should be called I get the following in the log: 但是,当应该调用onActivityResult()方法时,我在日志中得到以下内容:

V/ActivityManager(   60): Finishing activity: token=HistoryRecord{442cc518 com.myActivity/.primitives.ReturnResultActivity}, result=3, data=Intent { (has extras) }
V/ActivityManager(   60): Adding result to HistoryRecord{442a3988 com.mainActivity.sample/.MainActivity} who=null req=500 res=3 data=Intent { (has extras) }

Additional information 附加信息

Every entity is in a separate project (I have 3 projects interacting with each other) and therefore they are all running in their own process. 每个实体都在一个单独的项目中(我有3个项目相互交互),因此它们都在自己的流程中运行。

The MainActivity is started from the Service with the following code: MainActivity从服务启动,代码如下:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(activityInfo.packageName, activityInfo.name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
          | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);

Some of the Code 一些守则

Return Result Activity: 返回结果活动:

public class ReturnResultActivity extends SomeActivity {
    private static final boolean DEBUG = true;
    private static final String TAG = "ReturnResultActivity";

    protected void onBind() {
        // We want to monitor the service for as long as we are
        // connected to it.
        Message msg = Message.obtain(null,
                CentralService.MSG_LISTEN);
        msg.replyTo = mMessenger;

        try {
            mService.send(msg);
        } catch (RemoteException e) {
            // In this case the service has crashed before we could even
            // do anything with it; we can count on soon being
            // disconnected (and then reconnected if it can be restarted)
            // so there is no need to do anything here.
            e.printStackTrace();
        }
    }

    /** this method is called eventually after doUnbindService is called **/
    protected void onUnbind() {
        if (DEBUG) Log.i(TAG, "Unbinding and finishing"); //I can tell this gets called when I run it
        if (data != null) {
            setResult(CentralService.MSG_SPEECH_RECOGNIZED, data);
        }
        finish();
    }

    Intent data;
    protected boolean receiveMessage(Message msg) {
        if (DEBUG) Log.i(TAG, "Incoming Message to Listener: " + msg.what);

        switch (msg.what) {
            case ASRManager.MSG_SPEECH_RECOGNIZED:
                data = new Intent();
                Bundle bundle = msg.getData();
                bundle.setClassLoader(getClassLoader());
                data.putExtra(ASRManager.TOKEN_PARCEL_KEY, bundle.getParcelable(ASRManager.TOKEN_PARCEL_KEY));
                if (DEBUG) Log.i(TAG, "Received speech, setting result.");
                doUnbindService();

                return true;
            default:
                return false;
        }
    }


}

Main Activity 主要活动

public class MainActivity extends CustomActivity {
    private static final boolean DEBUG = true;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent("com.custom.returnresultaction");
        startActivityForResult(listenIntent, 500);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (DEBUG) Log.i(TAG, "Got activity result " + resultCode); //this doesn't get called

    }
}

No stack traces show up in my LogCat window. 我的LogCat窗口中没有显示堆栈跟踪。 Please help! 请帮忙!

EDIT: Interesting new information. 编辑:有趣的新信息。 If I then launch MainActivity from the launcher I see the following in logcat: 如果我然后从启动器启动MainActivity,我在logcat中看到以下内容:

I/ActivityManager(   60): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mainActivity.sample/.MainActivity }
V/ActivityManager(   60): Sending result to HistoryRecord{44253568 com.android.launcher/com.android.launcher2.Launcher} (index -1)
I/ActivityManager(   60): Starting Activity Unchecked Locked
V/ActivityManager(   60): com.mainActivity.sample.MainActivity Audio Activity Found
V/ActivityManager(   60): Delivering results to HistoryRecord{442c0310 com.mainActivity.sample/.MainActivity}: [ResultInfo{who=null, request=500, result=3, data=Intent { (has extras) }}]
V/ActivityThread(  327): Handling send result to ActivityRecord{440de270 token=android.os.BinderProxy@440ddca0 {com.mainActivity.sample/com.mainAcitivty.sample.MainActivity}}
V/ActivityThread(  327): Delivering result to activity ActivityRecord{440de270 token=android.os.BinderProxy@440ddca0 {com.mainActivity.sample/com.mainActivity.sample.MainActivity}} : ResultInfo{who=null, request=500, result=3, data=Intent { (has extras) }}
I/MainActivity(  327): Got activity result 3

My theory is that the task is not running which contains the MainActivity because the message is received from the CentralService. 我的理论是,没有运行包含MainActivity的任务,因为消息是从CentralService接收的。 Any thoughts? 有什么想法吗? Any idea how to switch to the correct task. 知道如何切换到正确的任务。 (note even though this may seem bad practice and disruptive to switch tasks on a user and bring another Activity to the forefront this is what I want to do. This is because this will eventually be running on a custom version of android which will make all this not disruptive.) (注意即使这可能看起来很糟糕,也可能会破坏用户切换任务并将另一个Activity带到最前端,这就是我想要做的。这是因为这最终将在自定义版本的android上运行,这将使所有这不是破坏性的。)

After coming back to this question 2 years later the issue is because the activity is launched in a new task. 2年后回到这个问题后,问题是因为活动是在一项新任务中启动的。 (I set this in the intent I fire). (我把它设置在我开火的意图中)。 Starting an activity for result that launches in a new task immediately will return -1. 为立即启动新任务的结果启动活动将返回-1。

After you call 打电话后

startActivityForResult(listenIntent, 500);

In the com.custom.returnresultaction class, are you including this code? 在com.custom.returnresultaction类中,您是否包含此代码?

setResult(RESULT_CANCELED, i);

or setResult(RESULT_OK, i);

and call finish();

You need to set the result and call finish(). 您需要设置结果并调用finish()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:onActivityResult()未调用startActivityForResult - Android: onActivityResult() is not calling for startActivityForResult Android:startActivityForResult停止调用onActivityResult - Android: startActivityForResult stopped calling onActivityResult Android-在onActivityResult中调用startActivityForResult有多安全? - Android - how safe is calling startActivityForResult within onActivityResult? 侦听器内部的startactivityforresult()未调用onActivityResult() - startactivityforresult() inside a listener not calling onActivityResult() 调用startActivityForResult永远不会传递给onActivityResult - Calling startActivityForResult never pass to onActivityResult 从onActivityResult调用新的StartActivityForResult - Calling a new StartActivityForResult from onActivityResult 返回startActivityForResult而不调用onActivityResult - startActivityForResult returning without calling onActivityResult Android startActivityForResult,setResult,onActivityResult未调用 - Android startActivityForResult, setResult, onActivityResult not called Android - startActivityForResult 立即触发 onActivityResult - Android - startActivityForResult immediately triggering onActivityResult 我正在调用 startActivityForResult 并发现未调用 onActivityResult - I'm calling startActivityForResult and finding that onActivityResult is not called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM