安卓:startActivityForResult不叫onActivityResult [英] Android: startActivityForResult not calling onActivityResult

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

问题描述

我的设置

  • 在自己的进程中运行的服务,CentralService
  • 在调用startActivityForResult()的一个活动,MainActivity
  • 正在启动对结果的活动,ReturnResultActivity

我试图做

  1. 开始ReturnResultActivity并将其绑定到服务(注册其处理程序)
  2. 让任何其他活动,要运行运行
  3. 当它接收来自服务的消息:
    • 从服务解除绑定
    • 在完成()
    • 的setResult()
      1. 调用了MainActivity的onActivityResult()方法

使用Log.i我已经证实,步骤1到3发生。然而,当onActivityResult()方法应该叫我得到的日志中以下内容:

  V / ActivityManager(60):整理活动:令牌= HistoryRecord {442cc518 com.myActivity / .primitives.ReturnResultActivity},结果= 3,数据= {意向(有临时演员)}
V / ActivityManager(60):添加结果HistoryRecord {442a3988 com.mainActivity.sample / .MainActivity}谁= NULL REQ = 500解析度= 3的数据= {意向(有临时演员)}
 


更多信息

每一个实体是在一个单独的项目(我有3个项目相互交融),因此,他们都在自己的进程中运行。

该MainActivity是从服务开始与下面code:

 意向意图=新的意图(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(意向);
 


的一些code

返回结果的活动:

 公共类ReturnResultActivity扩展SomeActivity {
    私有静态最终布尔DEBUG = TRUE;
    私有静态最后字符串变量=ReturnResultActivity;

    保护无效onBind(){
        //我们想要监视,只要我们的服务
        //连接到它。
        消息味精= Message.obtain(空,
                CentralService.MSG_LISTEN);
        msg.replyTo = mMessenger;

        尝试 {
            mService.send(MSG);
        }赶上(RemoteException的E){
            //在这种情况下,服务已经崩溃之前,我们竟会
            //用它做任何事情;我们可以指望很快被
            //断开(并重新连接,如果它可以被重新启动)
            //所以没有必要在这里做任何事情。
            e.printStackTrace();
        }
    }

    / **此方法被称为最终doUnbindService后调用** /
    保护无效onUnbind(){
        如果(调试)Log.i(TAG,解除绑定和整理); //我可以告诉这个被调用,当我运行
        如果(数据!= NULL){
            的setResult(CentralService.MSG_SPEECH_RECOGNIZED,数据);
        }
        完();
    }

    意图数据;
    保护布尔receiveMessage(信息MSG){
        如果(调试)Log.i(TAG,传入消息监听器:+ msg.what);

        开关(msg.what){
            案例ASRManager.MSG_SPEECH_RECOGNIZED:
                数据=新的意图();
                束束= msg.getData();
                bundle.setClassLoader(getClassLoader());
                data.putExtra(ASRManager.TOKEN_PARCEL_KEY,bundle.getParcelable(ASRManager.TOKEN_PARCEL_KEY));
                如果(调试)Log.i(TAG,接收到的语音,设置的结果。);
                doUnbindService();

                返回true;
            默认:
                返回false;
        }
    }


}
 

主要活动

 公共类MainActivity扩展CustomActivity {
    私有静态最终布尔DEBUG = TRUE;
    私有静态最后字符串变量=MainActivity;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        意向意图=新的意图(com.custom.returnresultaction);
        startActivityForResult(listenIntent,500);

    }

    @覆盖
    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(调试)Log.i(TAG,得到了活动的结果+结果code); //这个不会被调用

    }
}
 

没有堆栈跟踪显示在我的LogCat中的窗口。 请帮帮忙!

编辑:有趣的新信息。如果我再从启动我看到了下面的logcat中推出MainActivity:

  I / ActivityManager(60):启动活动:意向{行为= android.intent.action.MAIN猫= [android.intent.category.LAUNCHER] FLG = 0x10200000 CMP = COM .mainActivity.sample / .MainActivity}
V / ActivityManager(60):发送结果HistoryRecord {44253568 com.android.launcher / com.android.launcher2.Launcher}(指数-1)
I / ActivityManager(60):首发活动未选中锁定
V / ActivityManager(60):com.mainActivity.sample.MainActivity音频活动实测
V / ActivityManager(60):创造业绩,以HistoryRecord {442c0310 com.mainActivity.sample / .MainActivity}:[ResultInfo {谁= NULL,请求= 500,结果= 3,数据= {意向(有临时演员)}}]
V / ActivityThread(327):处理发送结果ActivityRecord {440de270 token=android.os.BinderProxy@440ddca0 {com.mainActivity.sample / com.mainAcitivty.sample.MainActivity}}
V / ActivityThread(327):交付结果的活动ActivityRecord {440de270 token=android.os.BinderProxy@440ddca0 {com.mainActivity.sample / com.mainActivity.sample.MainActivity}}:ResultInfo {谁= NULL,请求= 500,结果= 3,数据= {意向(有临时演员)}}
I / MainActivity(327):GOT活动成果3
 

我的理论是,任务没有运行包含MainActivity因为消息是从CentralService接收。有什么想法吗?任何想法如何切换到正确的任务。 (注意,尽管这看起来是不好的做法和破坏性上的用户切换任务,并把另一个活动的最前沿,这是我想做的事情。这是因为这将最终在Android的定制版本,这将使所有运行这不是破坏性的。)

解决方案

回来这个问题,经过2年后,这个问题是由于活动是在一个新的任务中启动。 (我设置的意图我火)。启动一个启动一个新任务的活动的结果将立即返回-1。

My Setup

  • A Service running in its own process, CentralService
  • An activity that calls startActivityForResult(), MainActivity
  • The activity that is being started for result, ReturnResultActivity

What I'm trying to do

  1. Start ReturnResultActivity and have it bind to the service (register its handler)
  2. Let whatever other activities want to run run
  3. When it receives a message from the service:
    • Unbind from the Service
    • finish()
    • setResult()

      1. Have the MainActivity's onActivityResult() method called

Using Log.i I have confirmed that steps 1 to 3 happen. However when the onActivityResult() method should be called I get the following in the log:

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.

The MainActivity is started from the Service with the following code:

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. Please help!

EDIT: Interesting new information. If I then launch MainActivity from the launcher I see the following in 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. 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.)

解决方案

After coming back to this question 2 years later the issue is because the activity is launched in a new task. (I set this in the intent I fire). Starting an activity for result that launches in a new task immediately will return -1.

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

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