Activity started with startActivityForResult () does not return to the calling activity

I have 3 actions - A , B and C.

In a nutshell, Activity A starts Activity B, then A also starts Activity C and expects a result from C, but never gets it.

Here is the application workflow:

  • Activity A starts when the application starts and starts Activity B (not for the result, only startActivity ()) in onCreate.

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); startActivity(new Intent(getApplicationContext(), ActivityB.class)); } 
  • Then action A also starts action C later in the code, this time for the result using startActivityForResult () and Activity A also has onActivityResult.

     Intent intent = new Intent(getApplicationContext(), ActivityC.class); startActivityForResult(intent, 0); 

    and

     protected void onActivityResult(int requestCode, int resultCode, Intent data) { ... } 
  • Activity C uses setResult () and finish () to return some data that should return to Activity A, since Activity A is called startActivityForResult ().

     Intent intent = new Intent(); intent.putExtra("encryption", encryption); setResult(56, intent); finish(); 

BUT the workflow does not work in step 3 - Activity A onActivityResult is never called (none of them matters B), although Activity A is the one that runs C for the result. Not sure if activity B interferes with the exchange of A and C or what could be a problem. Any help is greatly appreciated.

+7
source share
3 answers

I never managed to get this to work, so I used the handler instead to return the data to the desired action.

UPDATE After that, I found out that the real reason this doesn't work is because I had android:noHistory="true" for the caller / receiver action of A in the manifest. Removing android:noHistory="true" fixed, but if you need this to be true, then handlers are a good workaround.

+20
source

I do not think you should use getApplicationContext() in intent.

From the developer's website.

 getApplicationContext() Return the context of the single, global Application object of the current process. 

When you startActivityForResult() , it tries to return to the activity specified in the intent that you provide as the global context of the application.

If you have an ActivityB, you should call it as

 Intent intent = new Intent(ActivityB.this, ActivityC.class); startActivityForResult(intent, 0); 

Then it will try to return to ActivityB when ActivityC is executed.

+1
source

You can try:

 if (getParent() == null) { setResult(RESULT_OK, dataTobePassback); } else { getParent().setResult(RESULT_OK, dataTobePassback); } 

dataTobePassback is an Intent that carries stuff you might need to return to the calling activity

-2
source

All Articles