How to load Xamarin Forms in Android after calling SetContentView?

keahiekeahie Member ✭✭

When I render a custom Android page with SetContentView and then want to load a Xamarin Forms App (e.g. LoadApplication(new App());) nothing happens. The debug prints a lot of lines but no error. Has someone experience with a problem similar like this?

Best Answer

  • LandLuLandLu Xamurai
    Accepted Answer

    When you want to load the forms page, create a new activity to display the Forms pages.
    Modify your OnCreate like:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    
        var shoudLoadForms = Intent.GetBooleanExtra("ShouldLoadForms", false);
        if (shoudLoadForms)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
    
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
        else
        {
            SetContentView(Resource.Layout.Main);
    
            textView = FindViewById<TextView>(Resource.Id.main_text_view);
            button = FindViewById<Button>(Resource.Id.main_button);
    
            UpdateUI();
        }            
    }
    

    And the navigate method could be like this:

    button.Click += delegate
    {
        Android.Content.Intent intent = new Android.Content.Intent(this, typeof(MainActivity));
        intent.PutExtra("ShouldLoadForms", true);
        intent.SetFlags(Android.Content.ActivityFlags.ClearTask | Android.Content.ActivityFlags.NewTask);
        StartActivity(intent);
    };
    

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    You should post some code
  • keahiekeahie Member ✭✭

    Here is my MainActivity from the Android project. Ignore the strings on the bottom. They are written in German.

    `
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
    readonly string[] PermissionsLocation = {
    Manifest.Permission.AccessCoarseLocation,
    Manifest.Permission.AccessFineLocation
    };

        TextView textView;
        Button button;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            SetContentView(Resource.Layout.Main);
    
            textView = FindViewById<TextView>(Resource.Id.main_text_view);
            button = FindViewById<Button>(Resource.Id.main_button);
    
            UpdateUI();
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            switch (requestCode)
            {
                case 0:
                    if (grantResults[0] == 0)
                    {
                        LoadApplication(new App());
                    }
                    else
                    {
                        AskForPermissions();
                    }
                    UpdateUI();
                    break;
                default:
                    break;
            }
        }
    
        void AskForPermissions()
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.M)
            {
                return;
            }
    
            if (CheckSelfPermission(Manifest.Permission.AccessFineLocation) == Permission.Granted)
            {
                LoadApplication(new App());
                return;
            }
    
            RequestPermissions(PermissionsLocation, 0);
        }
    
        void UpdateUI()
        {
            if (HasPermissions(PermissionsLocation))
            {
                textView.Text = "Willkommen zum LocationNavigator!";
                button.Text = "Zur Karte";
                button.Click += delegate
                {
                    LoadApplication(new App());
                };
                return;
            }
            textView.Text += "\nDiese App benötigt deine Position um funktionieren zu können!";
            button.Text = "Erteile Berechtigung";
            button.Click += delegate
            {
                AskForPermissions();
            };
        }
    
        bool HasPermissions(string[] list)
        {
            foreach (var permission in list)
            {
                if (CheckSelfPermission(permission) == Permission.Denied)
                {
                    return false;
                }
            }
            return true;
        }
    }`
    
  • keahiekeahie Member ✭✭

    Ok, here is my code of the Android project. Just ignore the strings on the bottom. They are written in German.

    `public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

    {
        readonly string[] PermissionsLocation = {
            Manifest.Permission.AccessCoarseLocation,
            Manifest.Permission.AccessFineLocation
        };
    
        TextView textView;
        Button button;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            SetContentView(Resource.Layout.Main);
    
            textView = FindViewById<TextView>(Resource.Id.main_text_view);
            button = FindViewById<Button>(Resource.Id.main_button);
    
            UpdateUI();
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            switch (requestCode)
            {
                case 0:
                    if (grantResults[0] == 0)
                    {
                        LoadApplication(new App());
                    }
                    else
                    {
                        AskForPermissions();
                    }
                    UpdateUI();
                    break;
                default:
                    break;
            }
        }
    
        void AskForPermissions()
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.M)
            {
                return;
            }
    
            if (CheckSelfPermission(Manifest.Permission.AccessFineLocation) == Permission.Granted)
            {
                LoadApplication(new App());
                return;
            }
    
            RequestPermissions(PermissionsLocation, 0);
        }
    
        void UpdateUI()
        {
            if (HasPermissions(PermissionsLocation))
            {
                textView.Text = "Willkommen zum LocationNavigator!";
                button.Text = "Zur Karte";
                button.Click += delegate
                {
                    LoadApplication(new App());
                };
                return;
            }
            textView.Text += "\nDiese App benötigt deine Position um funktionieren zu können!";
            button.Text = "Erteile Berechtigung";
            button.Click += delegate
            {
                AskForPermissions();
            };
        }
    
        bool HasPermissions(string[] list)
        {
        ...
        }
    }`
    
  • keahiekeahie Member ✭✭

    Ok, here is my code from the Android project. Just ignore the strings on the bottom. They are written in German.

    `public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
    readonly string[] PermissionsLocation = {
    Manifest.Permission.AccessCoarseLocation,
    Manifest.Permission.AccessFineLocation
    };

        TextView textView;
        Button button;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            SetContentView(Resource.Layout.Main);
    
            textView = FindViewById<TextView>(Resource.Id.main_text_view);
            button = FindViewById<Button>(Resource.Id.main_button);
    
            UpdateUI();
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            switch (requestCode)
            {
                case 0:
                    if (grantResults[0] == 0)
                    {
                        LoadApplication(new App());
                    }
                    else
                    {
                        AskForPermissions();
                    }
                    UpdateUI();
                    break;
                default:
                    break;
            }
        }
    
        void AskForPermissions()
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.M)
            {
                return;
            }
    
            if (CheckSelfPermission(Manifest.Permission.AccessFineLocation) == Permission.Granted)
            {
                LoadApplication(new App());
                return;
            }
    
            RequestPermissions(PermissionsLocation, 0);
        }
    
        void UpdateUI()
        {
            if (HasPermissions(PermissionsLocation))
            {
                textView.Text = "Willkommen zum LocationNavigator!";
                button.Text = "Zur Karte";
                button.Click += delegate
                {
                    LoadApplication(new App());
                };
                return;
            }
            textView.Text += "\nDiese App benötigt deine Position um funktionieren zu können!";
            button.Text = "Erteile Berechtigung";
            button.Click += delegate
            {
                AskForPermissions();
            };
        }
    
        bool HasPermissions(string[] list)
        {
            foreach (var permission in list)
            {
                if (CheckSelfPermission(permission) == Permission.Denied)
                {
                    return false;
                }
            }
            return true;
        }
    }`
    
  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    When you want to load the forms page, create a new activity to display the Forms pages.
    Modify your OnCreate like:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    
        var shoudLoadForms = Intent.GetBooleanExtra("ShouldLoadForms", false);
        if (shoudLoadForms)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
    
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
        else
        {
            SetContentView(Resource.Layout.Main);
    
            textView = FindViewById<TextView>(Resource.Id.main_text_view);
            button = FindViewById<Button>(Resource.Id.main_button);
    
            UpdateUI();
        }            
    }
    

    And the navigate method could be like this:

    button.Click += delegate
    {
        Android.Content.Intent intent = new Android.Content.Intent(this, typeof(MainActivity));
        intent.PutExtra("ShouldLoadForms", true);
        intent.SetFlags(Android.Content.ActivityFlags.ClearTask | Android.Content.ActivityFlags.NewTask);
        StartActivity(intent);
    };
    
  • keahiekeahie Member ✭✭

    @LandLu said:
    When you want to load the forms page, create a new activity to display the Forms pages.
    Modify your OnCreate like:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    
        var shoudLoadForms = Intent.GetBooleanExtra("ShouldLoadForms", false);
        if (shoudLoadForms)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
    
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
        else
        {
            SetContentView(Resource.Layout.Main);
    
            textView = FindViewById<TextView>(Resource.Id.main_text_view);
            button = FindViewById<Button>(Resource.Id.main_button);
    
            UpdateUI();
        }            
    }
    

    And the navigate method could be like this:

    button.Click += delegate
    {
        Android.Content.Intent intent = new Android.Content.Intent(this, typeof(MainActivity));
        intent.PutExtra("ShouldLoadForms", true);
        intent.SetFlags(Android.Content.ActivityFlags.ClearTask | Android.Content.ActivityFlags.NewTask);
        StartActivity(intent);
    };
    

    Thanks. This solution works perfectly

Sign In or Register to comment.