.NET MAUI: can't run ActivityResult callback on Android

rodionovid 80 Reputation points
2024-03-08T10:55:08.8966667+00:00

Hello. I have .net maui application that serves as launcher for another multiplatform application. This launcher should install/update that another application and then do some actions (update ui, delete temporary files,..). The problem is that I can't find a way to run such a callback.

In my app DownloadCompletedCallback runs after downloading all necessary files:

        private void DownloadCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            try
            {
                ZipFile.ExtractToDirectory(zipPath, cachePath, true);
                File.Delete(zipPath);
                InstallApp install = new InstallApp(apkPath, versionRef, ref VersionLabel);
                install.Run();
            }
            catch (Exception ex)
            {
                StatusLabel.Text = ($"Error installing or updating app: {ex}");
            }
        }

This method extracts all files (apk in case of Android) and then handles installation via partial InstallApp class. Run method of this class executes installation and it has different implemintations depending on specific platform (I used the approach described in docs: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/invoke-platform-code?view=net-maui-8.0). In case of Android the method looks like this:

    partial class InstallApp
    {
        partial void _Run()
        {
            var context = Android.App.Application.Context;
            Java.IO.File apkFile = new Java.IO.File(_apkPath);
            Intent intent = new Intent(Intent.ActionView);
            var uri = Microsoft.Maui.Storage.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".fileProvider", apkFile);
            intent.SetDataAndType(uri, "application/vnd.android.package-archive");
            intent.AddFlags(ActivityFlags.NewTask);
            intent.AddFlags(ActivityFlags.GrantReadUriPermission);
            intent.AddFlags(ActivityFlags.ClearTop);
            intent.PutExtra(Intent.ExtraNotUnknownSource, true);
            Platform.CurrentActivity.StartActivity(intent);
        }
    }
}

Here I am using StartActivity method. Problems start here because I want to get access to callback function after successful apk installation. There are two ways do that as I found:

  1. Using StartActivityForResult and onActivityResult callback. In that case I need to call StartActivityForResult instead of StartActivity with some code:

Platform.CurrentActivity.StartActivityForResult(intent, 1);

And then I need to override the callback and catch the end of my activity via requestCode:

        protected override void OnActivityResult(int requestCode, int resultCode, Android.Content.Intent intent)
        {
            if (requestCode == 1)
            {
				// some actions after installing/updating the app
                WebClient webClient = new WebClient();
                Version remoteVersion = new Version(webClient.DownloadString(_versionRef));
                _VersionLabel.Text = remoteVersion.ToString();
                File.Delete(_apkPath);
            }
        }

The method can be overridden only in case when InstallApp class inherits Action class from Android.App. But InstallApp is a partial class that shared between different platforms (not only Android), so it can't inherit from Android specific class.

  1. Using RegisterForActivityResult method from Android Activity Result API. The problem is that I can't find the way to import this method in android specific section of my app in order to use it inside of my InstallApp class methods. I found not too much about this case and it didn't help:https://stackoverflow.com/questions/74199643/cant-find-registerforactivityresult-in-xamarin-maui-c-sharp-libraries

I am really stuck on this problem and can't move forward, so I will appreciate any help!

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-03-11T05:35:22.9233333+00:00

    Hello,

    The method can be overridden only in case when InstallApp class inherits Action class from Android.App. But InstallApp is a partial class that shared between different platforms (not only Android), so it can't inherit from Android specific class.

    In fact, you could do this through an instance of a MainActivity without inheritance.

    Please refer to the code below:

    public class MainActivity : MauiAppCompatActivity
    {
        public static MainActivity Instance { get; private set; }
    
    
       protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Instance = this;
        }
    
    
       protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
        }
    }
    
    //After that, you can execute the callback directly using the MainActivity.
    MainActivity.Instance.StartActivityForResult(intent, 1);
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.