为什么 startActivityForResult 的结果在活动真正开始之前出现?

发布于 2024-11-28 05:48:07 字数 891 浏览 0 评论 0原文

我有以下代码来启动活动:

public boolean onOptionsItemSelected(final MenuItem item) { 
    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
    case MENU_PREFS:
        startActivityForResult(new Intent(this, PreferencesActivity.class),1);
            break;
    case MENU_ABOUT:
        startActivityForResult(new Intent(this, AboutActivity.class),2);
            break;
        }
    return false;
    }

并使用以下代码来捕获结果:

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

        switch (requestCode){

        case 1:

        case 2:
            if (resultCode==RESULT_CANCELED)
                finish();
        }

但 onActivityResult 在活动(结果)开始之前触发。它的工作原理很奇怪。首先,它使用相应的 requestCode(在我的例子中为 1 或 2)和 resultCode=0 返回结果,以便我的主要活动完成。就在相应的活动(结果)真正开始之后。啥?

I have the following code to start activities:

public boolean onOptionsItemSelected(final MenuItem item) { 
    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
    case MENU_PREFS:
        startActivityForResult(new Intent(this, PreferencesActivity.class),1);
            break;
    case MENU_ABOUT:
        startActivityForResult(new Intent(this, AboutActivity.class),2);
            break;
        }
    return false;
    }

and following to catch results:

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

        switch (requestCode){

        case 1:

        case 2:
            if (resultCode==RESULT_CANCELED)
                finish();
        }

but onActivityResult triggers before activity (for result) starts. It works weird. First it retuns result with corresponding requestCode (1 or 2 in my case) with resultCode=0 so my main activity finishes. And right after that corresponding activity (for result) really starts. WTH?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2024-12-05 05:48:07

这是 Android 中的一个已知问题或功能。请参阅线程,其中涵盖了类似的问题

It is a know issue or feature in Android. See this thread which covers similar problem

半山落雨半山空 2024-12-05 05:48:07

您的活动正在启动的活动中的代码是什么样的?被调用活动中确定何时执行 SetResult(Result.OK,1) 返回原始调用者的标准是什么?在您调用 Finish() 或执行 SetResult 指令之前,您所调用的活动不会返回。

我有运行良好的代码。

        if (buttonSignOn.Text == "Proceed")
        {
            mAppFeatureMenuRetCD = "99";
            Intent ProcessAppFeatureMenu = new Intent();
            ProcessAppFeatureMenu.SetClass(this, typeof(AppFeatureMenu));
            ProcessAppFeatureMenu.PutExtra("CallingActName", "BPM_Activity1");
            ProcessAppFeatureMenu.PutExtra("AppFeatMenuStatusCode", "01");
            ProcessAppFeatureMenu.PutExtra("AppFeatMenuRetCd", "00");
            StartActivityForResult(ProcessAppFeatureMenu, Process_AppFeature_Menu);
            return;
        }




     public string ReturnToCallingActivity(Intent x, string y, string z)
     {

         x.PutExtra("CredStatCode", strBPMCredStatCode);
         x.PutExtra("RetActName", "ProgramSignOnStatus");
         x.PutExtra("MiscStuff", strCallingActMiscStuff);
         switch (y)
         {
             case "01":
                 SetResult(Result.Ok, x);
                 break;
             case "02":
                 SetResult(Result.Canceled,x);
                 break;
             default:
                 SetResult(Result.FirstUser, x);
                 break;
         }

         //Finish();
         return conOK;
     }

What does the code look like in the activities your activity is starting? What is the criteria in the called activity that determines when to do the SetResult(Result.OK,1) to return to the original caller? Your called activity will not return until you call Finish() or execute the SetResult instructions.

I have code that is working fine.

        if (buttonSignOn.Text == "Proceed")
        {
            mAppFeatureMenuRetCD = "99";
            Intent ProcessAppFeatureMenu = new Intent();
            ProcessAppFeatureMenu.SetClass(this, typeof(AppFeatureMenu));
            ProcessAppFeatureMenu.PutExtra("CallingActName", "BPM_Activity1");
            ProcessAppFeatureMenu.PutExtra("AppFeatMenuStatusCode", "01");
            ProcessAppFeatureMenu.PutExtra("AppFeatMenuRetCd", "00");
            StartActivityForResult(ProcessAppFeatureMenu, Process_AppFeature_Menu);
            return;
        }




     public string ReturnToCallingActivity(Intent x, string y, string z)
     {

         x.PutExtra("CredStatCode", strBPMCredStatCode);
         x.PutExtra("RetActName", "ProgramSignOnStatus");
         x.PutExtra("MiscStuff", strCallingActMiscStuff);
         switch (y)
         {
             case "01":
                 SetResult(Result.Ok, x);
                 break;
             case "02":
                 SetResult(Result.Canceled,x);
                 break;
             default:
                 SetResult(Result.FirstUser, x);
                 break;
         }

         //Finish();
         return conOK;
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文