maui icon indicating copy to clipboard operation
maui copied to clipboard

MAUI Android build crashes when app is reopened from background. It throws the exception: 'Window was already created.'

Open nsood9 opened this issue 1 year ago • 57 comments

Description

Exception: Window was already created. Source: Microsoft.Maui.Controls'

StackTrace " at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Created() in D:\a\_work\1\s\src\Controls\src\Core\Window\Window.cs:line 491\n at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_0(Activity activity, Bundle bun…" string

Steps to Reproduce

Create a Shell app with Flyout and bottom Tabs. Click the home button on device to move the app to the background. Now open the app again from background, it will throw the exception: Window was already created.

No app can be released with this kind of bug where app cannot be restarted from background which is very basic thing.

Link to public reproduction project repository

No response

Version with bug

8.0.0-rc.1.9171

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

No response

Did you find any workaround?

No, couldn't find any workaround yet. It is quite frustrating and don't know how to fix it.

Relevant log output

No response

nsood9 avatar Nov 11 '23 08:11 nsood9

Hi @nsood9. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Nov 12 '23 06:11 ghost

I tried to reproduce what you suggested, but I couldn't in an Android Emulator and on a Pixel 4a, both with a debugger attached and without, and in debug and release mode. I don't doubt you hit an issue, but I'm unsure if its due to a more specific combination of items contained within your application. Can you please create a repro project that can show this error?

drasticactions avatar Nov 12 '23 06:11 drasticactions

It is a huge project now, which has backend api project, datatbase project, admin project and also blazor web app. I'm just working on the mobile project which was started recently and it is crashing with this error "Window was already created". I think I will not be able to create a sample repo because this is huge project which number of other projects inside it, and it is a company's client project. But this issue has been reported by other developers also who all are facing this same issue, but I couldn't find any solution to it.

nsood9 avatar Nov 14 '23 00:11 nsood9

@nsood9 do you use Mopups plugin in project ? I have same exception when displaying popup and then when app is Activated.

arkadiuszokon avatar Nov 14 '23 14:11 arkadiuszokon

No, I'm not using any such plugin in my project. I'm just using Telerik UI for MAUI, Riok Mapperly and MAUI CommunityToolkit.

nsood9 avatar Nov 14 '23 21:11 nsood9

Yesterday, I updated to .Net 8 and tested the app hoping the issue might be resolved in the latest update. But, the issue still persist and the app crashes as soon as I reopen the android app from background. Has anyone faced this crash before and could find a solution for it? It's quite critical to fix this issue.

nsood9 avatar Nov 15 '23 21:11 nsood9

I can confirm that I have seen this crash get logged in AppCenter. This app has been migrated from 7.0, if that's relevant.

A fuller stack trace:

Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Created()
Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_0(Activity activity, Bundle bundle)
Microsoft.Maui.MauiAppCompatActivity.<>c__DisplayClass8_0.<OnPostCreate>b__0(OnPostCreate del)
Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[OnPostCreate](IServiceProvider services, Action`1 action)
Microsoft.Maui.MauiAppCompatActivity.OnPostCreate(Bundle savedInstanceState)
Android.App.Activity.n_OnPostCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState)
Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0)

BioTurboNick avatar Nov 18 '23 14:11 BioTurboNick

I have been experiencing the same issue since I have updated the app from .NET 7.0 to .NET 8.0. My app is using native MAUI UI, Shell navigation and MAUI Community Toolkit. I'm using a magic link to open the app from an email, this is when it crashes consistently on all Android emulators and works on iOS. Stack trace similar as @BioTurboNick's :

0xFFFFFFFFFFFFFFFF in Android.Runtime.RuntimeNativeMethods.monodroid_debugger_unhandled_exception
0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:13,5
0x1E in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:126,26
0x12 in Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Created at D:\a\_work\1\s\src\Controls\src\Core\Window\Window.cs:491,5
0xB in Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_0 at D:\a\_work\1\s\src\Core\src\Hosting\LifecycleEvents\AppHostBuilderExtensions.Android.cs:23,6
0xD in Microsoft.Maui.MauiAppCompatActivity. at D:\a\_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.Lifecycle.cs:61,104
0x1F in Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents<Microsoft.Maui.LifecycleEvents.AndroidLifecycle.OnPostCreate> at D:\a\_work\1\s\src\Core\src\LifecycleEvents\LifecycleEventServiceExtensions.cs:31,5
0x40 in Microsoft.Maui.MauiAppCompatActivity.OnPostCreate at D:\a\_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.Lifecycle.cs:61,4
0x11 in Android.App.Activity.n_OnPostCreate_Landroid_os_Bundle_ at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.App.Activity.cs:4488,4
0x9 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:125,5

wolfkonig avatar Nov 20 '23 15:11 wolfkonig

@wolfkonig I was doing some poc with sharing and I came across this issue as well. After digging I was able to fix it by adding LaunchMode = LaunchMode.SingleTask and below code in MainActivity

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    if (intent?.Action == Intent.ActionSend && intent.Type == "text/plain")
    {
        var data = intent?.ClipData?.GetItemAt(0);
        var text = data.Text;
    }
}

@drasticactions I was able to reproduce it by creating new .NET 8 project and adding below code [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] [IntentFilter(new[] { Intent.ActionView, Intent.ActionSend }, Categories =[Intent.CategoryDefault],DataMimeType = "text/plain")] Application just breaks with the above exceptions people mentioned after executing OnCreate

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    if (Intent?.Action == Intent.Action && Intent.Type == "text/plain")
    {
        var data = Intent?.ClipData?.GetItemAt(0);
        var text = data.Text;
    }
}

Hopefully this helps 👍

EDIT: Same exact error code works with .NET 7 with no issues :)

dragonking009 avatar Nov 21 '23 10:11 dragonking009

Bump - this is an issue when going from .NET 7 to .NET 8

This causes a crash.

Following is the stack trace already reported above.

System.InvalidOperationException: Window was already created
  ?, in void Window.Microsoft.Maui.IWindow.Created()
  ?, in void AppHostBuilderExtensions.OnConfigureLifeCycle(IAndroidLifecycleBuilder android)+(Activity activity, Bundle bundle) => { } [0]
  ?, in void MauiAppCompatActivity.OnPostCreate(Bundle savedInstanceState)+(OnPostCreate del) => { }
  ?, in void LifecycleEventServiceExtensions.InvokeLifecycleEvents<OnPostCreate>(IServiceProvider services, Action<OnPostCreate> action)
  ?, in void MauiAppCompatActivity.OnPostCreate(Bundle savedInstanceState)
  ?, in void Activity.n_OnPostCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState)
  ?, in void JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0)

patrickklaeren avatar Nov 28 '23 08:11 patrickklaeren

Facing Same Issue !!

What I am Doing is ->

First I Opens the App via AppActions, and Enters the MainPage.

Now I Go Back to launcher and Open the App Again Directly.... It Crashes and throwing the following Exception: " System.InvalidOperationException: 'Window was already created' "

the-netspider avatar Dec 11 '23 15:12 the-netspider

A lot of people has been facing this issue, but no solution has been provided yet. Has anyone found any work around for this crash? If yes, then please share, it will help everyone here. Thanks!

nsood9 avatar Dec 11 '23 22:12 nsood9

A lot of people has been facing this issue, but no solution has been provided yet. Has anyone found any work around for this crash? If yes, then please share, it will help everyone here. Thanks!

If you look a few posts up, changing the LaunchMode helped one person. In that mode, Android uses a different path than the Create path when the app is revisited (https://developer.android.com/guide/topics/manifest/activity-element#lmode). But I haven't tried it myself.

BioTurboNick avatar Dec 12 '23 01:12 BioTurboNick

I face the same issue, here is stacktrace from Firebase Crashlytics:

Fatal Exception: android.runtime.JavaProxyThrowable: [System.InvalidOperationException]: Window was already created
       at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Created()
       at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions+<>c.<OnConfigureLifeCycle>b__2_0()
       at Microsoft.Maui.MauiAppCompatActivity+<>c__DisplayClass8_0.<OnPostCreate>b__0()
       at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents()
       at Microsoft.Maui.MauiAppCompatActivity.OnPostCreate()
       at Android.App.Activity.n_OnPostCreate_Landroid_os_Bundle_()
       at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V()
       at crc6488302ad6e9e4df1a.MauiAppCompatActivity.n_onPostCreate(MauiAppCompatActivity.java)
       at crc6488302ad6e9e4df1a.MauiAppCompatActivity.onPostCreate(MauiAppCompatActivity.java:97)
       at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1286)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2988)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3084)
       at android.app.ActivityThread.-wrap11()
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1781)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:210)
       at android.app.ActivityThread.main(ActivityThread.java:7080)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:523)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:863)

I still can't reproduce this crash, but it hold me back from publishing my app.

prok155 avatar Dec 12 '23 10:12 prok155

Today, by chance, when I was reporting another bug #19374, I managed to reproduce this issue. You should do following steps:

Steps to Reproduce

  1. Download NewMauiApp1 and NewMauiApp2.
  2. Start debbuging NewMauiApp2
  3. Launch app NewMauiApp1
  4. Press 'Start second app'
  5. Visual studio will throw following exception: System.InvalidOperationException: 'Window was already created'

Link to public reproduction project repository

https://github.com/prok155/NewMauiApp1 https://github.com/prok155/NewMauiApp2

When you will try to reproduce it, you should do it on a physical device, because on emulators NewMauiApp2 doesn't launch from NewMauiApp1 (PackageManager doesn't find a package, I don't know why).

prok155 avatar Dec 12 '23 20:12 prok155

A lot of people has been facing this issue, but no solution has been provided yet. Has anyone found any work around for this crash? If yes, then please share, it will help everyone here. Thanks!

If you look a few posts up, changing the LaunchMode helped one person. In that mode, Android uses a different path than the Create path when the app is revisited (https://developer.android.com/guide/topics/manifest/activity-element#lmode). But I haven't tried it myself.

__

I tried your suggestion, I have tested on all singletop, singletask, singleInstance etc. but same story everytime..!! Changing android:launchMode has no effect on this issue, I think its a MAUI Bug. One more thing. as @prok155 said above, In my case also its working fine on Visualstudio Emulator but crashes only on physical devices.

the-netspider avatar Dec 14 '23 05:12 the-netspider

A lot of people has been facing this issue, but no solution has been provided yet. Has anyone found any work around for this crash? If yes, then please share, it will help everyone here. Thanks!

If you look a few posts up, changing the LaunchMode helped one person. In that mode, Android uses a different path than the Create path when the app is revisited (https://developer.android.com/guide/topics/manifest/activity-element#lmode). But I haven't tried it myself.

__

I tried your suggestion, I have tested on all singletop, singletask, singleInstance etc. but same story everytime..!! Changing android:launchMode has no effect on this issue, I think its a MAUI Bug. One more thing. as @prok155 said above, In my case also its working fine on Visualstudio Emulator but crashes only on physical devices.

Same here, it happens when I start my app on a physical device. Emulator however works without problems.

SeeUnsharp avatar Dec 16 '23 11:12 SeeUnsharp

I have similar issue at android - not able to tell the exact source of the exception is as discussed here. However, to me it happens when window is requested from foreground service via notification. I could simulate that when open the window and then try to open it again from the notification.

[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: Window was already created [mono-rt] at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Created() in D:\a_work\1\s\src\Controls\src\Core\Window\Window.cs:line 491 [mono-rt] at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_0(Activity activity, Bundle bundle) in D:\a_work\1\s\src\Core\src\Hosting\LifecycleEvents\AppHostBuilderExtensions.Android.cs:line 23 [mono-rt] at Microsoft.Maui.MauiAppCompatActivity.<>c__DisplayClass8_0.<OnPostCreate>b__0(OnPostCreate del) in D:\a_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.Lifecycle.cs:line 61 [mono-rt] at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[OnPostCreate](IServiceProvider services, Action`1 action) in D:\a_work\1\s\src\Core\src\LifecycleEvents\LifecycleEventServiceExtensions.cs:line 31 [mono-rt] at Microsoft.Maui.MauiAppCompatActivity.OnPostCreate(Bundle savedInstanceState) in D:\a_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.Lifecycle.cs:line 61 [mono-rt] at Android.App.Activity.n_OnPostCreate_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 4488 [mono-rt] 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 125

At android, I am a bit suffering with events risen quickly one after another when clicked at control. Instead of doubleclik it is recognized as two clicks. Might have some connection to occasional crash when trying to open already opened window?

I4-PJ avatar Dec 20 '23 20:12 I4-PJ

I'm having the same issue when .NET 8 MAUI app is already running, same error: System.InvalidOperationException: 'Window was already created'

How can we check the progress of this bug report?

plppp2001 avatar Dec 27 '23 16:12 plppp2001

Hi,

I'm trying to figure things out with this, but just wondering if we need to do something like this (see attached screen shot) Screenshot 2023-12-28 011719

https://github.com/redth/maui.applinks.sample

plppp2001 avatar Dec 28 '23 00:12 plppp2001

@plppp2001 Is it working for you? Are you able to fix this crash on Android with the mentioned workaround?

nsood9 avatar Dec 28 '23 20:12 nsood9

@plppp2001 Is it working for you? Are you able to fix this crash on Android with the mentioned workaround?

Haven't tried this yet, I'm currently frustrated with the iOS AppLinks Universal Links for .net 8 MAUI app; (if I have any updates I'll post in the future though.... )

https://github.com/redth/maui.applinks.sample

plppp2001 avatar Dec 28 '23 20:12 plppp2001

I also have a similar error but on iOS. Below is the log

System.InvalidOperationException: Window was already activated
   at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Activated()
   at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_3(UIApplication app)
   at Microsoft.Maui.MauiUIApplicationDelegate.<>c__DisplayClass14_0.<OnActivated>b__0(OnActivated del)
   at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[OnActivated](IServiceProvider services, Action`1 action)
   at Microsoft.Maui.MauiUIApplicationDelegate.OnActivated(UIApplication application)
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94

quangkhuongduy0195 avatar Jan 05 '24 03:01 quangkhuongduy0195

@wolfkonig I was doing some poc with sharing and I came across this issue as well. After digging I was able to fix it by adding LaunchMode = LaunchMode.SingleTask and below code in MainActivity

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    if (intent?.Action == Intent.ActionSend && intent.Type == "text/plain")
    {
        var data = intent?.ClipData?.GetItemAt(0);
        var text = data.Text;
    }
}

@drasticactions I was able to reproduce it by creating new .NET 8 project and adding below code [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] [IntentFilter(new[] { Intent.ActionView, Intent.ActionSend }, Categories =[Intent.CategoryDefault],DataMimeType = "text/plain")] Application just breaks with the above exceptions people mentioned after executing OnCreate

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    if (Intent?.Action == Intent.Action && Intent.Type == "text/plain")
    {
        var data = Intent?.ClipData?.GetItemAt(0);
        var text = data.Text;
    }
}

Hopefully this helps 👍

EDIT: Same exact error code works with .NET 7 with no issues :)

I have this same issue. NET MAUI .NET Android when opening an app link. I made the changes in this comment and the issue is resolved for me, but then OnAppLinkRequestReceived wasn't being hit in App.xaml.cs. So here's my solution:

protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        if (intent?.Action == Intent.ActionView)
        {
            var data = intent?.Data?.ToString();
            if (data is not null)
            {
                HandleAppLink(data);
            }
        }
    }
    
static void HandleAppLink(string url)
    {
        if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri))
        {
            App.Current?.SendOnAppLinkRequestReceived(uri);
        }
    }

Note, this code is also needed if you want the app to trigger OnAppLinkRequestReceived when the app is not running in the background. (from https://github.com/Redth/MAUI.AppLinks.Sample)

#elif ANDROID
            lifecycle.AddAndroid(android =>
            {
                android.OnCreate((activity, bundle) =>
                {
                    var action = activity.Intent?.Action;
                    var data = activity.Intent?.Data?.ToString();

                    if (action == Android.Content.Intent.ActionView && data is not null)
                    {
                        HandleAppLink(data);
                    }
                });
            });
#endif

I also tried this workaround, which did not work.

I also have the Mopups issue that someone mentioned above. If mopups is open when the app is minimized, when it is reopened, it crashes.

donatellijl12 avatar Jan 13 '24 19:01 donatellijl12

Screenshot 2024-01-14 171836

adding LaunchMode = LaunchMode.SingleTask and below code in MainActivity

"adding LaunchMode = LaunchMode.SingleTask and below code in MainActivity" this has fixed my issue (.NET MAUI 8) Opening links from the outlook email app and it opens my MAUI ANdroid app (when fully closed or open in background now without any errors.) thanks.

@nsood9

plppp2001 avatar Jan 14 '24 16:01 plppp2001

I used SingleTask it fixed the problem when I send something from WhatsApp, but when I used other apps like file manager, I got the error that other people mentioned! I used the code bellow in .net 8:

[Activity(Theme = "@style/MyTheme", LaunchMode =LaunchMode.SingleTask, MainLauncher = true,
 ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[IntentFilter(new[] { Intent.ActionSend },
                      Categories = [Intent.CategoryDefault],
                      DataMimeType = "image/*")]

mabdollahiasl avatar Jan 25 '24 12:01 mabdollahiasl

I used SingleTask it fixed the problem when I send something from WhatsApp, but when I used other apps like file manager, I got the error that other people mentioned! I used the code bellow in .net 8:

[Activity(Theme = "@style/MyTheme", LaunchMode =LaunchMode.SingleTask, MainLauncher = true,
 ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[IntentFilter(new[] { Intent.ActionSend },
                      Categories = [Intent.CategoryDefault],
                      DataMimeType = "image/*")]

Just wondering by what you mean, file manager?

plppp2001 avatar Jan 25 '24 12:01 plppp2001

I used SingleTask it fixed the problem when I send something from WhatsApp, but when I used other apps like file manager, I got the error that other people mentioned! I used the code bellow in .net 8:

[Activity(Theme = "@style/MyTheme", LaunchMode =LaunchMode.SingleTask, MainLauncher = true,
 ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[IntentFilter(new[] { Intent.ActionSend },
                      Categories = [Intent.CategoryDefault],
                      DataMimeType = "image/*")]

Just wondering by what you mean, file manager?

if I select a photo in file manager apps like android "Files" and then select my app in share list, then I got the error "Window is already created". it is interesting that if I select a photo in WhatsApp and then share it to my app, I got no error.

mabdollahiasl avatar Jan 25 '24 12:01 mabdollahiasl

When you remove the SingleTask, does it work inversed and what I mean is the other way around does it work


From: Morteza Abdollahiasl @.> Sent: Thursday, January 25, 2024 1:30:20 PM To: dotnet/maui @.> Cc: Paul Astro @.>; Mention @.> Subject: Re: [dotnet/maui] MAUI Android build crashes when app is reopened from background. It throws the exception: 'Window was already created.' (Issue #18692)

I used SingleTask it fixed the problem when I send something from WhatsApp, but when I used other apps like file manager, I got the error that other people mentioned! I used the code bellow in .net 8:

[Activity(Theme = @.**/MyTheme", LaunchMode =LaunchMode.SingleTask, MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] [IntentFilter(new[] { Intent.ActionSend }, Categories = [Intent.CategoryDefault], DataMimeType = "image/")]

Just wondering by what you mean, file manager?

if I select a photo in file manager apps like android "Files" and then select my app in share list, then I got the error "Window is already created". it is interesting that if I select a photo in WhatsApp and then share it to my app, I got no error.

— Reply to this email directly, view it on GitHubhttps://github.com/dotnet/maui/issues/18692#issuecomment-1910107196, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AK2EVSRGEQSFX4CRCWW54ITYQJF5ZAVCNFSM6AAAAAA7HDKZSSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJQGEYDOMJZGY. You are receiving this because you were mentioned.Message ID: @.***>

plppp2001 avatar Jan 25 '24 14:01 plppp2001

After a lot of trying, I finally could fix it by using another activity. We can add another activity like code bellow:

[IntentFilter([Intent.ActionSend],
                   Categories = [Intent.CategoryBrowsable, Intent.CategoryDefault, Intent.ActionView],
                   DataMimeType = "text/*")]
[Activity(Theme = "@style/MyTheme", MainLauncher =false,Exported = true, LaunchMode = LaunchMode.SingleTask, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density, ScreenOrientation = ScreenOrientation.Portrait)]

public class ShareActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
         if (Intent?.Action == Intent.ActionSend && Intent.Type.StartsWith("text/"))
        {
          // add your code to process the text or other data that sent to your app
        }
        Finish(); //close this activity
        StartActivity(typeof(MainActivity)); // start MainActivity
    }
}

Just be sure that set Exported = true and ShareActivity must inherits from AppCompatActivity You should add ShareActivity.cs in platforms/Android folder.

mabdollahiasl avatar Jan 26 '24 08:01 mabdollahiasl