Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maui Android intent filter ActionView for MimeTypes not working #22261

Closed
sven-s opened this issue May 7, 2024 · 8 comments
Closed

Maui Android intent filter ActionView for MimeTypes not working #22261

sven-s opened this issue May 7, 2024 · 8 comments
Assignees
Labels
area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events platform/android 🤖 s/needs-attention Issue has more information and needs another look s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@sven-s
Copy link

sven-s commented May 7, 2024

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 sven-s added the t/bug Something isn't working label May 7, 2024
Copy link
Contributor

github-actions bot commented May 7, 2024

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.

@ninachen03 ninachen03 added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels May 8, 2024
@ninachen03
Copy link

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

@sven-s
Copy link
Author

sven-s commented May 8, 2024

@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.

@PureWeen PureWeen self-assigned this May 8, 2024
@PureWeen PureWeen added this to the Backlog milestone May 8, 2024
@PureWeen PureWeen added the area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events label May 8, 2024
@PureWeen
Copy link
Member

PureWeen commented May 8, 2024

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

@sven-s
Copy link
Author

sven-s commented May 9, 2024

@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?

@PureWeen
Copy link
Member

@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 PureWeen added the s/needs-info Issue needs more info from the author label May 10, 2024
@sven-s
Copy link
Author

sven-s commented May 13, 2024

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

@dotnet-policy-service dotnet-policy-service bot added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author labels May 13, 2024
@PureWeen
Copy link
Member

@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.

@github-project-automation github-project-automation bot moved this from Todo to Done in MAUI SDK Ongoing May 17, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events platform/android 🤖 s/needs-attention Issue has more information and needs another look s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
Status: Done
Development

No branches or pull requests

4 participants