Search code examples
c#xamarin.formsxamarin.androidxamarin.ios

Navigate from one app to another using Xamarin


I need to Navigate from one app to another in Xamarin.Forms.

Tried this but didn't work

await Xamarin.Essentials.Launcher.OpenAsync("myapp://com.companyname.myapp1");


Solution

  • Suppose the package name of the app you want to open is com.xamarin.secondapp.

    Then you need to add IntentFilter and Exported =true to MainActivity.cs of the app you want to open(com.xamarin.secondapp).

    Just as follows.

        [Activity(Label = "SearchBarDemos", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,Exported =true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
        [
        IntentFilter
        (
            new[] { Android.Content.Intent.ActionView },
            Categories = new[]
                {
                    Android.Content.Intent.CategoryDefault,
                    Android.Content.Intent.CategoryBrowsable
                },
            DataSchemes = new[] { "myapp" }
        )
    ]
        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
                LoadApplication(new App());
            }
        }
    

    Note: remember to add code DataSchemes = new[] { "myapp" }.

    For the current app, you need to add queries tag for the app you want to open in file manifest.xml.

    For example:

      <?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.openappapp1">
        <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
        <application android:label="OpenappApp1.Android" android:theme="@style/MainTheme">
    
          </application>
          <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    
          <queries>
                <package android:name="com.xamarin.secondapp" />
          </queries>
    </manifest>
    

    And the code is:

        private void Button_Clicked(object sender, EventArgs e) 
        {
            OpenSecondApp();
        }
    
        public async void OpenSecondApp()
        {
    
            var supportsUri = await Launcher.CanOpenAsync("myapp://");
            if (supportsUri)
                await Launcher.OpenAsync("myapp://com.xamarin.secondapp");
    
        }