{"record":{"id":"87f47d4145fbca3a","repo":"AvaloniaUI/Avalonia","slug":"main-activity-must-implement-iactivityresulthandle-87f47d","errorCode":null,"errorMessage":"Main activity must implement IActivityResultHandler interface.","messagePattern":"Main activity must implement IActivityResultHandler interface\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Android/Avalonia.Android/Platform/Storage/AndroidStorageProvider.cs","lineNumber":231,"sourceCode":"            .PutExtra(Intent.ExtraAllowMultiple, options.AllowMultiple);\n\n        intent = TryAddExtraInitialUri(intent, options.SuggestedStartLocation);\n\n        var pickerIntent = Intent.CreateChooser(intent, options.Title ?? \"Select folder\");\n\n        var uris = await StartActivity(pickerIntent, false);\n        return uris.Select(u => new AndroidStorageFolder(_activity, u, false)).ToArray();\n    }\n\n    private async Task<List<AndroidUri>> StartActivity(Intent? pickerIntent, bool singleResult)\n    {\n        var resultList = new List<AndroidUri>(1);\n        var tcs = new TaskCompletionSource<Intent?>();\n        var currentRequestCode = PlatformSupport.GetNextRequestCode();\n\n        if (!(_activity is IActivityResultHandler mainActivity))\n        {\n            throw new InvalidOperationException(\"Main activity must implement IActivityResultHandler interface.\");\n        }\n\n        mainActivity.ActivityResult += OnActivityResult;\n        _activity.StartActivityForResult(pickerIntent, currentRequestCode);\n\n        var result = await tcs.Task;\n\n        if (result != null)\n        {\n            // ClipData first to avoid issue with multiple files selection.\n            if (!singleResult && result.ClipData is { } clipData)\n            {\n                for (var i = 0; i < clipData.ItemCount; i++)\n                {\n                    var uri = clipData.GetItemAt(i)?.Uri;\n                    if (uri != null)\n                    {\n                        resultList.Add(uri);","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Android/Avalonia.Android/Platform/Storage/AndroidStorageProvider.cs#L213-L249","documentation":"Thrown by AndroidStorageProvider.StartActivity when the host _activity is not an IActivityResultHandler. Storage pickers (file/folder open/save) rely on StartActivityForResult and need the Activity to route OnActivityResult back into Avalonia via the IActivityResultHandler interface; if the host activity doesn't implement it, the result callback cannot be wired.","triggerScenarios":"Launching a storage picker (TryGetFileFromPathAsync is not it — these are picker flows like OpenFilePicker/SaveFilePicker) when the application's main Activity does not implement IActivityResultHandler. Typically because the Activity does not extend AvaloniaMainActivity/AvaloniaFragmentActivity, which supply the interface.","commonSituations":"Custom Activity that extends a plain Android.App.Activity or third-party base instead of Avalonia's; apps integrating Avalonia views inside a non-Avalonia host Activity; libraries that host an AvaloniaView without using the provided base activities.","solutions":["Make your host Activity extend AvaloniaMainActivity<TApp> (or AvaloniaFragmentActivity) so it implements IActivityResultHandler.","If you must use a custom base, implement IActivityResultHandler manually and forward OnActivityResult to the ActivityResult event.","Do not call the storage picker before the Activity is fully created/initialized."],"exampleFix":"// before\npublic class MainActivity : Activity { ... } // not an IActivityResultHandler\n\n// after\npublic class MainActivity : AvaloniaMainActivity<MyApp> { ... } // implements IActivityResultHandler","handlingStrategy":"validation","validationCode":"// confirm the activity can handle results before launching a picker\nif (activity is not IActivityResultHandler)\n    throw new InvalidOperationException(\"Host Activity must extend AvaloniaMainActivity/AvaloniaFragmentActivity.\");\nvar files = await storageProvider.OpenFilePickerAsync(options);","typeGuard":"static bool CanHandleActivityResult(Android.App.Activity a) => a is IActivityResultHandler;","tryCatchPattern":null,"preventionTips":["Extend AvaloniaMainActivity<TApp> or AvaloniaFragmentActivity for the host Activity.","If using a custom base, implement IActivityResultHandler and raise ActivityResult from OnActivityResult.","Do not call storage pickers before the Activity is created.","Keep the storage provider usage confined to Avalonia-hosted activities."],"tags":["android","activity","storage","picker","startup"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}