As promised in my previous post (way too long ago, I know! I’m Sorry!!!), I would talk more about the Activities for Android applications in .Net (By Xamarin).
I want to start with one of the most important parts of the application, the Activities.

[Activity]
public class MyActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
       .....
    }
}

This is the very basic of an Activity. It inherits from Activity and has an attribute Activity. The Activity base class provides many overridable methods, like onCreate, which is called when the Activity is created (think of the Page_Load in WebForm pages).

The Activity attribute has many interesting properties, some of them are listed below:

  • Label, this provides the label for the application, shown in the Android app screen
  • MainLauncher, this is an indication whether the Activity is the main activity and will be started on application startup.
  • Icon, Sets the icon for the application
  • Theme, sets the theme for the application, this can be a custom theme or a predefined theme.
  • ConfigurationChanges, sets which changes are handled by the application itself.
[Activity(Label = "My application",
          MainLauncher = true,
          Icon = "@drawable/icon",
          ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize,
          Theme = "@android:style/Theme.NoTitleBar")]
public class MyActivity : Activity
{
       .....
}

This is an example of the Activity with attributes. No custom code is needed to make this work.

The ConfigurationChanges is set to “Orientation” and “ScreenSize”, this means that orientation changes are handled by the Activity. I use this a lot with WebViews, this stops the application from reloading the page when the orientation changes.

The Theme “NoTitleBar” removes the titlebar from the application. This makes it possible to create your own title bar.
A list of all available predefined themes can be found here

The following two tabs change content below.
I'm a software developer from Utrecht. Interested in DDD, continuous delivery, new technologies & frameworks.

Latest posts by Vincent Keizer (see all)