Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity started with startActivityForResult() not returning to calling Activity

I have 3 Activities - 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:

  1. Activity A is launched on app startup and starts Activity B (not for result, just startActivity()) in onCreate.

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startActivity(new Intent(getApplicationContext(), ActivityB.class));
    }
    
  2. Activity A then also starts Activity C later in code, this time for 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) {
       ...
    }
    
  3. Activity C uses setResult() and finish() to return some data, which should go back to Activity A, because Activity A called startActivityForResult().

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

BUT the workflow falls silent at step 3 - Activity A's onActivityResult is never called (neither is B's for that matter), even though Activity A is the one starting C for result. Not sure if Activity B is getting in the way of A and C's communication or what the problem could be. Any help is much appreciated.

like image 442
Gady Avatar asked Mar 13 '12 17:03

Gady


People also ask

How can I get result from startActivityForResult?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

Can startActivityForResult still be used?

It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.

What can I use instead of startActivityForResult?

But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

Is startActivityForResult deprecated?

onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.


2 Answers

I was never able to get this to work, so I ended up using a Handler instead to return the data to the necessary Activity.

UPDATE: After running into this again, I found out that the real reason this wasn't working is because I had android:noHistory="true" for the calling/receiving Activity A in the manifest. Removing android:noHistory="true" fixed it, but if you need it to be true, then Handlers are a good workaround.

like image 128
Gady Avatar answered Oct 19 '22 03:10

Gady


I don't think you should use getApplicationContext() in the intent.

From the developer website.

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

When you you startActivityForResult() it tries to return to the activity specified in the intent, which you are providing as the global application context.

If you have an ActivityB then you should call it like

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

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

like image 20
triggs Avatar answered Oct 19 '22 03:10

triggs