Question

I have two standalone applications. Application A and Application B. I want to start activity in application B from Application A and get the results back. I can call activity in Application B from A using Action But Cannot get back to Application A after finishing the activity. OnActivityResult in A is never called. Following is the code.

public void onClickBtnToApplicationB(View v) {
        try {
            final Intent intent = new Intent(Intent.ACTION_MAIN, null);
            final ComponentName cn = new ComponentName("pakacagename","package.class");
            intent.setComponent(cn);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            startActivityForResult(intent, REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
        //handle Exception
        } 
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        switch (requestCode) {
            case REQUEST_CODE:
               handleResult(resultCode, intent);
               break;
        }
    }

    public void handleResult(int resultCode, Intent intentResult) {
        switch (resultCode) {
            case RESULT_OK:
                String Result = intentResult.getStringExtra("RESULT");
                // I need Results from Application B here..
                break;

             case RESULT_CANCELED:
                break;
        }
      }

Application B :

Intent s = new Intent(1.this,2.class);
startActivityForResult(s, REQUEST_CODE_B);
protected void onActivityResult(int requestCode, int resultCode, Intent intentResult) {     
    switch(requestCode){
        case REQUEST_CODE_B:
            handleResult(resultCode, intentResult);
    }
}

public void handleResult(int resultCode, Intent intentResult) {
    switch (resultCode) {
    case RESULT_OK:
        String scanResult = intentResult.getStringExtra("RESULT");
        Intent newintent = new Intent();
        newintent.putExtra("RESULT", scanResult);
        setResult(Activity.RESULT_OK, newintent);
        finish();
        break;

    case RESULT_CANCELED:
        break;
}
Was it helpful?

Solution

Perhaps I'm missing something. Application A seems to start an Activity in Application B for a result and implements onActivityResult. The way you've constructed the Intent to send has some issues, but let's skip that for a moment.

As far as I can tell, the form of the Intent you send is immaterial, because Application B is never looking at it. The receiving Activity should have a call to getIntent(). Based on the incoming ACTION, it set a result code and return Intent, call setResult(), and then call finish(). Your code isn't doing this; instead, it seems like you're trying to start Application A by calling startActivityForResult().

I understand why you might have tried this, but the sequence should be:

  1. Activity A wants to start Activity B (regardless of Activity B's application).
  2. Activity A constructs Intent X that matches Activity B.
  3. Activity A calls startActivityForResult(X).
  4. Activity B comes to the foreground.
  5. The method onResume() in Activity B calls getIntent(). This ensures that Activity B can look at Intent X even if B was already running.
  6. Activity B does the action that was requested by Activity A.
  7. When done, Activity B puts its results in Intent Y.
  8. Activity B calls setResult(result_code, Y);
  9. Activity B calls finish(), which sends the result back to Activity A.
  10. The method onActivityResult() in Activity is triggered.

The result Intent Y is not sent by startActivityForResult; instead, it's send by a combination of setResult() and finish().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top