4

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?

Stan
  • 6,511
  • 8
  • 55
  • 87

2 Answers2

3

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

Community
  • 1
  • 1
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
1

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