dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.24k stars 1.76k forks source link

App is not redirecting when the app opens from Intent.ActionSend action #19665

Open DharunAR opened 10 months ago

DharunAR commented 10 months ago

Description

I am trying to share a picture or files from my android mobile to my MAUI app. When selecting the MAUI App, in MainActivity.cs -> OnCreate method i have added a Condition if the Intent.Action == Intent.ActionSEND, I am redirecting my app to different page. image

But the redirection is not happening if the app is already opened, and it opens the same page with the old app instance. This was working in Xamarin. I am not sure what i am missing here.

Steps to Reproduce

  1. Download the repo project.
  2. Build and run the app.
  3. On the button click, it will navigate to the page called LoadingPage.
  4. Minimize the app.
  5. Open your photos or files. Click on share icon and select our MAUI app.
  6. The app will open the new instance but it will still show the loadingPage. The redirection code i have written in OnCreate is getting executed but still it opens the old instance page. Not sure whats going wrong.

I am following the same approach when i was using Xamarin Forms

Link to public reproduction project repository

https://github.com/DharunAR/ShareFromMobile/

Version with bug

7.0.101

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12 and Android 13

Did you find any workaround?

No

Relevant log output

No response

DharunAR commented 10 months ago

Can someone help me out with the workaround or resolution on how to get rid of this issue? This is blocking our migration from Xamarin to MAUI.

Schullebernd commented 10 months ago

Why are you creating a new App object inside the OnCreate Event? At this time the app was already started and you „only“ need to care about the navigation to the shared page. Maybe it‘s a good idea to follow the approach from the official docu https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/appmodel/app-actions?view=net-maui-8.0&tabs=android

DharunAR commented 10 months ago

Thanks for your suggestion @Schullebernd. The above AppActions looks to be all together different. The current documentation will help to include Actions on the app long press. I am trying to Share a file from my mobile device to app(Please check my repo project and repo steps). On Sharing, the Intent.Action will be ActionSend, for ActionSend intent i am trying to redirect to different page.

This was working with Xamarin Forms, I am not sure what has been changed with MAUI and i didnt get any proper doco as well. It would be helpful if someone guide me on how to resolve this issue.

Schullebernd commented 10 months ago

It's a little strange behavior. When start "send to App" on Android, a new instance of the app seems to be created (or at least a new window instance). Anyway, I can reproduce the problem. Since you are using AppShell in you project, I recommend to use the shell navigation features with GoToAsync method to navigate. In your example you need to register a route for the sharedPage in the AppShell constructor:

namespace ShareFromMobile
{
    public partial class AppShell : Shell
    {
        public AppShell()
        {
            InitializeComponent();

            // Register the route for the shared page
            Routing.RegisterRoute("MainPage/sharedPage", typeof(SharePage));
        }
    }
}

And then start navigating to this page in the onCreate method of your main activity:

namespace ShareFromMobile
{
    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
    [IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeTypes = new[] { "image/jpeg", "text/plain", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/pdf", "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/msword" })]
    public class MainActivity : MauiAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (Intent.Action == Intent.ActionSend)
            {
                Shell.Current.GoToAsync("//MainPage/sharedPage");
            }
        }
    }
}

This works on your example project with one exception. When the app is already alive, the back navigation doesn't navigate back to the main page. But I'm not so familiar with AppShell and don't use it in my project. Maybe it's only a routing config issue.

DharunAR commented 10 months ago

@Schullebernd The GoToAsync call is working but it fails when i am trying to redirect from SharePage.

 [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
 [IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeTypes = new[] { "image/jpeg", "text/plain", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/pdf", "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/msword" })]
 [IntentFilter(
    new[] { Microsoft.Maui.ApplicationModel.Platform.Intent.ActionAppAction },
    Categories = new[] { Intent.CategoryDefault })]
 public class MainActivity : MauiAppCompatActivity
 {
     protected override void OnCreate(Bundle savedInstanceState)
     {
         base.OnCreate(savedInstanceState);
         if (Intent.Action == Intent.ActionSend)
         {
             Shell.Current.GoToAsync(nameof(SharePage));
         }
     }
 }

On SharePage i have a label control with tapgesturerecognizer, it fails with Object reference error when redirecting from there.

Xaml Code

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Net8Sample.SharePage"
             Title="SharePage">
    <VerticalStackLayout>
        <Label 
            Text="Welcome to Share Page"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
        <Label Text="Tap event" TextColor="Black" FontSize="20">
            <Label.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
            </Label.GestureRecognizers>
        </Label>
    </VerticalStackLayout>
</ContentPage>

.cs code

public partial class SharePage : ContentPage
{
    public SharePage()
    {
        InitializeComponent();
    }

    private async void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
    {
        await Shell.Current.GoToAsync(nameof(LoadingPage));
    }
}

It's very easy to replicate. Can you help me out with this?

kevinxufei commented 7 months ago

Verified this issue with Visual Studio 17.10.0 Preview 3, Can repro issue with sample project on NET7,An exception occurred when testing on net8 (8.0.20). Screenshot 2024-04-18 100744