2

I'm using Xamarin C# here.

I have an Android application, app A, and I would like to launch it from a second application, app B, passing some data into it. This will dictate which operations to perform and what data to return.

When app A is finished, I would like to return said data to app B.

Currently, app B launches app A via StartActivityForResult, and awaits the response with OnActivityResult:

PackageManager pm = PackageManager;
Intent intent = pm.GetLaunchIntentForPackage("Application.A");
intent.SetAction("Task_1");
Bundle bundle = new Bundle();
bundle.PutString("Activity", "BundleActivity");
StartActivityForResult(intent, (int)ActivityCode.Task1, bundle);

This correctly launches app A and allows me to determine that we need to do Task_1 (via the action, I've not tried the bundle).

When app A is done it performs the following:

var returnIntent = new Intent();
returnIntent.PutExtra("AppBData", the_data_obj);
SetResult(Result.Ok, returnIntent);
Finish();

This is detected by app B and caught in the OnActivityResult:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    switch ((ActivityCode)requestCode)
    {
        case ActivityCode.Task1:
        {
            if (data != null)
            {
                Java.Lang.Object dataReturned = data.GetParcelableExtra("AppBData");
            }
        }
        break;
        default:
            break;
    }
}

}

but 'data' is always null.

Any ideas please?

Chris Dafnis
  • 123
  • 4

1 Answers1

0

I have tested your way to start ApplicationB, and the value of resultCode is also Canceled.

So don't use Intent intent = pm.GetLaunchIntentForPackage("Application.A"); to get the Intent, use SetClassName method to select which component you will start:

Intent intent = new Intent();
intent.SetClassName("ApplicationB.ApplicationB", "ApplicationB.ApplicationB.MainActivity");
intent.PutExtra("Activity", "BundleActivity");
intent.PutExtra("Operation", "Task_1");
StartActivityForResult(intent,1);

And thanks @SushiHangover's answer, you need add Name="ApplicationB.ApplicationB.MainActivity" above your ApplicationB's ManiActivity.

I have post the demo on github, there are two applications, ApplicationA and ApplicationB, run ApplicationB and then run ApplicationA, the A will open B and there is a button in B, click it, you will back to A and get the data from B.

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • This works exactly as I needed, thank you! I only seem to be able to launch the main activity (Java.Lang.SecurityException: Permission Denial: starting Intent), but I can launch the correct one from there, so it's not a big problem. – Chris Dafnis Apr 20 '18 at 09:51