Get up to 80 % extra points for free! More info:

Lesson 2 - Xamarin Project Structure and the Application Life Cycle

In the previous lesson, Introduction to Xamarin, we explained what Xamarin is, what it allows us to do, installed Visual Studio, and set up the first project. In today's tutorial, we're going to take a look at the Xamarin project structure and the mobile application's life cycle.

Xamarin Project Structure

When we look at Solution Explorer, we can see the generated structure divided into 4 main parts (the names will vary according to the project name, I named mine FirstApp).

Solution Explorer with Xamarin Project in Visual Studio - Smartphone Apps in Xamarin and C# .NET

C# FirstApp

We'll focus on this part the most. Here we'll write common code used by all the platforms. We'll be most interested in the MainPage.xaml and MainPage.xaml.cs files. You can already guess that the MainPage.xaml file will be used to position the controls in the layout, and MainPage.xaml.cs to handle these controls using C# .NET.

FirstApp.Android

For Android, we'll be most interested in the following files and folders:

  • Connected Services - Connected services such as databases and so on will be located here. We'll get to this later in the course.
  • Properties - There's a very important file named AndroidManifest.xml. In this file, we set the minimum and target version of the API, the icon, and many other parameters of our application.
  • Resources/ - Here we'll place images for our application. Note the Drawable/ folders and their other variants. That's because mobile devices, like computers, have different resolutions and sizes, so we'll need to adjust the control layout and image resolutions to the size of each device's screen. For example, for a full HD tablet, we use the highest image resolution. On the other hand, we use the smallest one for a small phone so that the application doesn't take up too much memory and isn't slow.
  • MainActivity.cs - It's a file containing the default activity used when the application is started. We'll talk more about the activities below.

FirstApp.iOS

There'll be activities, images, and scripts specific for iOS. Since the principle is the same as for Android, so there's no need to describe it right now.

FirstApp.UWP

You'll only see this option if you selected UWP (Universal Windows Platform) during the installation. If you want to install this tool later, you can do so in the Visual Studio installer. Since the code for Windows Phone is in C# and doesn't require any further modifications in simple projects, we won't discuss it in the first lessons.

We'll explain the project structure in more detail during the practical lessons later in the course. It wouldn't make much sense to explain everything now because we don't have the necessary knowledge yet, and it might confuse us.

Activity

Activity refers to a specific screen of our application and defines its entire life cycle from opening / starting to closing / stopping. It contains several possible states and methods, which are called when it goes from one state of the life cycle to another. It's important to remember that each screen (page) of an application must have its own activity. For example:

  • When we launch the application, we see an animation with a logo first, it'll be the first activity.
  • Then the basic menu (the second activity).
  • We click the link in the menu to open a new screen (the third activity) and so on.

Each activity must have an OnCreate() method. Other methods are optional.

The Activity Life Cycle

Now we know that there are activities that must have the OnCreate() method and may have other methods as well. But when and why are all these methods called? You know that when you play a game on your phone and someone calls you, the game automatically pauses and you can see the incoming call. When the call ends, you can return to the paused game. I'm sure that everyone understands the meaning of the different states of the life cycle represented by the methods. Let's look at its graphical representation:

Mobile application life cycle - Smartphone Apps in Xamarin and C# .NET

Activity State

Each activity is always in one of the following states at a time:

  • Running - The activity has started successfully and is running in the foreground, so it's visible to the user.
  • Paused - The activity is visible, but it's, for example, covered by another activity. (Notifications of incoming SMS, calls, or full battery charge dialog while charging). The user doesn't get to this activity and can't work with it.
  • Stopped - The activity isn't visible, the user has no access to it, but its object hasn't been destroyed yet. The user will be able to return to it if it isn't destroyed, e.g. because of insufficient memory.
  • Killed - The activity is completely dead.

The following methods are called as the app goes through these states.

The OnCreate() method

It's the default method that starts first and initializes the activity's life cycle. For example, when the user launches the application, this method determines what images should be loaded, what components will be there, whether the application should run in full screen and so on. In short, it'll initialize the entire design and features as we designed it. It's the only method required!

The OnStart() method

It's called if the activity has been started for the first time (after OnCreate()) or activated after it was hidden (incoming SMS, system dialog, e.g. on a battery charge or another dialog). This method can't get user input.

The OnResume() method

It's called just before the activity is brought to the foreground (restart, first start or after the paused state), can get user input.

The OnPause() method

It's called before the activity is sent to the background. The system is given the permission to force-close the activity.

The OnStop() method

It's called when the activity is about to be stopped, the activity is not visible to the user.

The OnDestroy() method

It's called before the activity is destroyed.

The OnRestart() method

As shown in the previous diagram, if OnStop() was called and the activity restarts, OnRestart() is called, which is performed before OnStart().

In the next lesson, First Cross-Platform Application in Xamarin and C# .NET, we'll finally finish with the theory and program a simple application that responds to user input and displays a greeting dialog. So you got something to look forward to!


 

Previous article
Introduction to Xamarin
All articles in this section
Smartphone Apps in Xamarin and C# .NET
Skip article
(not recommended)
First Cross-Platform Application in Xamarin and C# .NET
Article has been written for you by Jaroslav Smrz
Avatar
User rating:
No one has rated this quite yet, be the first one!
Activities