Xamarin Android Application Component

In this tutorial i’ll explain each Xamarin android application component.How you can build these components and connect to each other using Intents.Xamarin Android Application framework helps you to create different type of android applications using some reusable app components.

Different Xamarin Android Applications Components are

  • Intent and Intent Filters
  • Activities
  • Services
  • Content Providers
  • App Widgets
  • Processes and Threads

Now let’s start with component explanation

Intent

An Intent is an abstract concept for some sort of operation that should be performed in the Android OS. It’s often used to launch external applications with the intent to do something, such as make a phone call, display a web page, or map an address. As such, an Intent generally has two pieces of information associated with it; first, what the intent is (as in make a phone call), and second, what data does the intent need (such as a phone number) to perform the intention.

var intent = new Intent(this,typeof(newactivity));
StartActivity(intent); 

There are three basic purposes of intent

  • To start an Activity
  • To start a Service
  • To deliver a Broadcast

What is Intent Filter?

Intent filter is an expression in app’s manifest file which find out the type of intents that any particular component wants to receive.

How to build Intent?

An Intent contain  information that the Android System uses to find out which component to start and also that information which recipient component uses to perform any action

Information contained in an intent is as follows

  • Component Name(name of the component to start by Intent)
  • Action(action to perform such as ACTION_VIEW,ACTION_SEND)
  • Data(data to be acted on by )
  • Category(kind of component handle by Intent)
  • Extras(contains some more information to share with another component or passing)
  • Flags(how to start an activity and what should do after starting that particular activity)

Activities

An Activity is an essential Xamarin android application component which provides  user interface screen which is used by any user to perform actions e.g dial number,play game etc.

Creating Activity

When you create new activity,it should first be extended with Activity class.It will look like this.

namespace task
{
	[Activity (Label = "nextScreen")]			
	public class nextScreen : Activity
	{
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Create your application here
		}
	}
}

How to start an Activity?

You can start an activity using this method StartActivity() by using intent because intent will hold the information which activity to start.e.g

var intent = new Intent(this,typeof(nextScreen));
StartActivity(intent); 

You can also pass data bread to another activity which you want to start.

var intent = new Intent(this,typeof(nextScreen));
intent.PutExtra ("MyData", "Data from Activity1");
StartActivity(intent); 

In above code key is the tag through which you will get information within another activity using getExtras method.

Activity LifeCycle of an Android Application

application component,xamarin activity lifecycle,xamarin,android,C# android,xamarin android

Above picture is taken from Xamarin Official Website.

Some description of activity lifecycle callback methods

  • onCreate()  this callback method is called when activity is being created.
  • onRestart() this callback method is called when an activity is stopped.
  • onStart() this callback method is called when an activity is about to become visible to the user.
  • onResume() this callback method is called before any activity starts interacting with the user.
  • onPause() this callback method is called when another activity become upfront to the user.(Activity being paused)
  • onStop() this callback method is called when no activity is interacting with the user means no more visible to the user.
  • onDestroy() this callback method is called before an activity is destroyed.
namespace task
{
	[Activity (Label = "nextScreen")]			
	public class nextScreen : Activity
	{
    @Override
   protected override void OnCreate (Bundle savedInstanceState){
        base.OnCreate(savedInstanceState);
        // The activity is being created.
    }
    @Override
    protected void OnStart() {
        base.OnStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void OnPause() {
        base.OnPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void OnStop() {
        base.OnStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void OnDestroy() {
        base.OnDestroy();
        // The activity is about to be destroyed.
    }
}
}

Services

An Xamarin andorid app component that from perform long operation at the background e.g network transaction,downloading contents for I/O.Link

There are two basic forms of Services

  • Started(Service is “started” when application component starts it by using this method startService and run indefinitely whether the component is destroyed which started it).
  • Bound(Service is “bound” when an application component binds that service using this method bindService() with components to interact with each others for communication or any other purposes).

A Service is implemented as follows

[Service]
public class MyService : Service
{
    ...
}

Content Provider

A ContentProvider encapsulates a data repository and provides an API to access it. The provider exists as part of an Android application that usually provides a UI for displaying/managing the data. The key benefit of using a content provider is enabling other applications to easily access the encapsulated data using a provider client object (called a ContentResolver).

App Widgets

App Widget is a small application interface which can be displayed or embedded in other applications.Like Home Screen which will keep you updated by receiving data from any type of server e.g. if you use Widget of Weather Application it will automatically add widget on your Home Screen.When it’s been installed and by the time it receive updates from server,display to the user.Now Home Screen you can called it App Widget Host.

Processes and Threads

When an app component get started.There is no other running component then Android system launch a process with in a single thread of an execution.

Basically by default all components of an application run in a same process

Process LifeCycle

There are five types of processes.

  • Foreground Process(run in foregroud according to the user requirements or what the user currently doing).
  • Visible Process(run in background which can affect user interface).
  • Service Process(a process which is used to run a service or a process that a run service by calling serviceStart()).
  • Background Process(A process which doesn’t have hold on any activity which can kill by the user or by the system).
  • Empty Process(process which doesn’ have hold on any application components.This process is used to restart the component when needed).

Threads

When an application is launched first time.It will create a thread which you call “main” and which will handle events dispatcher to UI widget.

InvokeOnMainThread ( () => {
    // manipulate UI controls
});

But there is a disadvantage for using this.When operation grows this will get complicated.Hard to maintain so to prevent from this situation best solution is AsyncTask.

AsyncTask:Asychronously process operations.

Different callback methods are

  • onPreExecute
  • doInBackground
  • onPostExecute

That’s it for this tutorial.I hope this might help you in understanding Xamarin android application component.

Thanks