maui icon indicating copy to clipboard operation
maui copied to clipboard

Maui Android intent filter ActionView for MimeTypes not working

Open sven-s opened this issue 9 months ago • 7 comments

Description

I want to handle pdf files with a Maui App on Android. For that I use an intent filter.

[IntentFilter([Intent.ActionView], Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable], DataMimeType = @"application/pdf")]

When running the maui app on a real device and selecting the maui app as a destination from another app (like the tesla app of handle the pdf receipt), the maui app crashes, if it is already open. The crash log looks like:

05-07 15:13:04.148 F/mono-rt (18920): [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: This window is already associated with an active Activity (OpenPDF.MainActivity). Please override CreateWindow on OpenPDF.App to add support for multiple activities https://aka.ms/maui-docs-create-windowor set the LaunchMode to SingleTop on OpenPDF.MainActivity.
05-07 15:13:04.148 F/mono-rt (18920):    at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(Activity activity, IApplication application, Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\ApplicationExtensions.cs:line 52
05-07 15:13:04.148 F/mono-rt (18920):    at Microsoft.Maui.MauiAppCompatActivity.OnCreate(Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.cs:line 35
05-07 15:13:04.148 F/mono-rt (18920):    at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.App.Activity.cs:line 3082
05-07 15:13:04.148 F/mono-rt (18920):    at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 121

LaunchMode is set up SingleTop, but that does not help.

Steps to Reproduce

I provided a minimal Maui app just with the intent filter attribute.

When you try to open(view) a pdf file with the installed maui app, the result will be

  • if the app is closed, the app starts without a crash
  • if the app is running in the background, the app crashes

Link to public reproduction project repository

https://github.com/sven-s/OpenPdf

Version with bug

8.0.21 SR4.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 13

Did you find any workaround?

No response

Relevant log output

No response

sven-s avatar May 07 '24 13:05 sven-s

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar May 07 '24 13:05 github-actions[bot]

Verified this issue with Visual Studio 17.10 Preview 6 (8.0.21 & 8.0.20). Cannot repro it. image

ninachen03 avatar May 08 '24 05:05 ninachen03

@ninachen03 What do you mean with "Cannot repro it"?

The app is crashing as well, isn't it? So you can repro it.

I am using a Mac with Rider an did run it on a Google Pixel 4a with Android 13.

sven-s avatar May 08 '24 05:05 sven-s

@sven-s if you override createwindow in your app.xaml and return a new window that should work around the issue for now.

PureWeen avatar May 08 '24 18:05 PureWeen

@PureWeen CreateWindow creates already a new window. I do not understand, how to return a new window, if the method already does the job. Do you have any example code?

sven-s avatar May 09 '24 07:05 sven-s

@sven-s can you record a video?

I tried to test your repro and it works fine for me

  • I backgrounded the app
  • Navigated to a PDF via the browser
  • Indicated I wanted your app to open it
  • and your app resumes

PureWeen avatar May 10 '24 22:05 PureWeen

@PureWeen I recorded a video with the crash: https://app.screencast.com/ESAs9L6kW09D9

sven-s avatar May 13 '24 07:05 sven-s

@sven-s So, I'm pretty sure this is the correct behavior and the recommendation from the exception is correct.

If you modify your App.xaml.cs file so it looks like this

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
    }

    protected override Window CreateWindow(IActivationState? activationState)
    {
        var windpw = new Window(new AppShell());
        return windpw;
    }

}

And then you run your app, you'll notice that the pdf is launched in a new activity.

here's the activity that is launched from the file app

image

and here's the original app

image

So, you basically have two active activities and thus need two separate windows to manage them. If you don't want to separate activities then you'll probably want to modify your IntentFilter so it opens that PDF in the already running activity. I'm not quite sure what those settings are but the android docs should have the info you need.

PureWeen avatar May 17 '24 19:05 PureWeen