Updating the TrackMyWalks.Android MainActivity

In this section, we need to make changes to the MainActivity class for our TrackMyWalks.Android project so that we can initialize our Xamarin.FormsMaps library, otherwise we won't be able to use any of the map features.

The MainActivity class begins immediately after your app launches. Once the main activity is running, it can launch other activities, which in turn can launch subactivities. When the application exits, it does so by terminating the main activity and any other activities terminate in a cascade form from within the main activity.

Let's take a look at how we can achieve this by following these steps:

  1. First, expand the TrackMyWalks.Android solution project that is contained within the TrackMyWalks solution.
  2. Next, double-click on the MainActivity.cs file, ensuring that it is displayed within the code editor, and enter the following highlighted code sections:
    //
// MainActivity.cs
// MainActivity class for the TrackMyWalks.Android Project
//
// Created by Steven F. Daniel on 14/05/2018
// Copyright © 2018 GENIESOFT STUDIOS. All rights reserved.
//
using Android.App;
using Android.Content.PM;
using Android.OS;

namespace TrackMyWalks.Droid
{
[Activity(Label = "TrackMyWalks", Icon = "@mipmap/icon",
Theme = "@style/MainTheme",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

base.OnCreate(bundle);

// Initialise our Xamarin.FormsMaps library
Xamarin.FormsMaps.Init(this, bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
}

In the preceding code snippet, we began by making changes to the OnCreate method within the MainActivity class to initialize our Xamarin.Forms.Maps library by making a call to the Xamarin.FormsMaps.Init() method, which accepts two parameters, the first one being the current class instance, and the second one being the bundle identifier. The OnCreate method is called whenever your application launches. If we forget to reference our Xamarin.FormsMaps.Init() method, the WalkDistancePage content page will cause issues when running the app.

For more information on the Xamarin.Forms.Maps library, as well as the various types of classes that are available, please refer to the Xamarin developer documentation at https://developer.xamarin.com/api/namespace/Xamarin.Forms.Maps/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.20.224.107