Reputation: 243
I am writing a Maui Android app which receives files shared from WhatsApp or Telegram.
There is only one activity defined and it is created as SingleTask
.
However whenever I resume the app via the task switcher, OnCreate
is called instead of OnNewIntent
. This in itself is not a problem, but when I share a file from say WhatsApp, an entire new instance of the app is created, so that I have two instances of the app running at the same time. This is causing crashes.
If I change the launchMode
to SingleTop
or SingleInstance
, I get exactly the same unwanted behaviour.
Here is my code. What is going on?
EDIT 1 I've been researching this more and one further thing that needs to be mentioned is that my app is being created as a child of the WhatsApp process. When I look at the task switcher, I have one task which is MyApp and one task that is WhatsApp but the WhatsApp's UI is another instance of MyApp which has been overlayed over the top of the normal WhatsApp UI.
Even more strange is both 'instances' of MyApp seem to be sharing the same activity instance values and window. So it is the same instance of the activity visually split into two in the Android system.
EDIT 2 Done more investigation, now when switching back and forth to the app I'm getting the follow error:
System.InvalidOperationException: 'This window is already associated with an active Activity (MyApp.MainActivity). Please override CreateWindow on MyApp.App to add support for multiple activities https://aka.ms/maui-docs-create-window or set the LaunchMode to SingleTop on MyApp.MainActivity.'
Again, changing the manifest
and MainActivity
to SingleTop
has no effect whatsoever.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mycompany.myapp">
<application android:allowBackup="true" android:icon="@mipmap/appicon"
android:supportsRtl="true" android:label="MyApp">
<activity android:exported="true"
android:name=".MainActivity"
android:launchMode="singleTask"
android:icon="@mipmap/appicon" />
</application>
...
Code
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true,
LaunchMode = LaunchMode.SingleTask,
NoHistory = true,
Exported = true,
TaskAffinity = "com.mycompany.myapp",
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation |
ConfigChanges.UiMode | ConfigChanges.ScreenLayout |
ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
//whatsapp & telegram send binaries as octet-stream
[IntentFilter(new[] { Android.Content.Intent.ActionSend },
Categories = new[] { Android.Content.Intent.CategoryDefault },
DataMimeType = @"application/octet-stream",
Icon = "@mipmap/appicon")]
public partial class MainActivity : MauiAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
if (Intent.Action == Intent.ActionSend || Intent.Action == Intent.ActionView)
HandleSendOrViewIntent();
}
//This method is NEVER called
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
if (Intent.Action == Intent.ActionSend || Intent.Action == Intent.ActionView)
HandleSendOrViewIntent(true);
}
}
Upvotes: 0
Views: 74