Search code examples
c#xamarin.formsxamarin.android

Xamarin Forms: jump to login screen after bluetooth connect/screen sharing


strange behaviour: I have a Xamarin Forms app. The login page is followed by a main menue navigation page after a web request:

var success= await viewModel.DoLoginAsync();
if (success)
{
  await Navigation.PushModalAsync(new NavigationPage(new MenuPage()));
}

Now I want to connect to a bluetooth device or share my screen with Samsung Smart View over the Android Settings Panel. As soon as the connection is established, the app returns to the login screen. Just as a manual call of PopModalAsync() would do.

Only tested on Samsung Devices. That's really annoying :)

Hope someone can help!

Edit: Just found out that inside MainActivity.cs, the OnDestroy() handler fires as soon as the connection is established. Is there a way to prevent this behaviour?


Solution

  • I'm using Xamarin Forms with Prism. I'm using a BT Scanner and had the same problem. It was caused because I didn't had the ConfigutionChanges for the Keyboard. This is part of my MainActivity:

    [Activity(
        Label = "Venta Movil",
        Icon = "@mipmap/ic_launcher",
        Theme = "@style/MainTheme",
        MainLauncher = false,
        ConfigurationChanges = ConfigChanges.ScreenSize | 
            ConfigChanges.Orientation | 
            ConfigChanges.UiMode | 
            ConfigChanges.ScreenLayout | 
            ConfigChanges.SmallestScreenSize |
            ConfigChanges.Keyboard |
            ConfigChanges.KeyboardHidden)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
    
            base.OnCreate(savedInstanceState);
    
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    
            // NuGet Initialization
            // FFImage
            FFImageLoading.Forms.Platform.CachedImageRenderer.Init(true);
    
            LoadApplication(new App());
        }
    
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);            
    
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    

    With the ConfigurationChanges attributes added, its works fine.