• Understand Android Activity, View, and Activity Lifecycle with Xamarin Android, Visual Studio, and C#

    by  • December 13, 2016 • Android with C# and .NET, Xamarin Mobile App Development • 0 Comments

    In our previous blog posts in this series, we have setup a basic structure of our solution with Xamarin.Android and a Core project. However, before we can get started with the real Android programming in C# and Visual Studio, we need to understand some basic concepts about Android Activities.

    Just like Windows or Forms in .NET and Windows programming, in Android we have Activities. Each Activity is a standalone screen, which can be linked programmatically with other activities to make a complete Android App. But an individual activity is a starting point. When we create a new Xamarin Android project, as in case of MyBookstore App, an Activity called MainActivity.cs is created by default. Each Activity is made of two things: 1. a code file, in our case C# or CS file 2. a view (.axml file which is an XML based file).

    1-mainactivity-with-view

    When an activity is run, the code file calls the view file and loads the view and when an element of the view is manipulated, the code behind that element is executed. We’ll explain these things in detail, but this is just to give you some basic idea.

    2-mainactivity-view-in-designer-mode

    Each activity is inherited from Activity class which contains some common functionality which can be used across the activities. When we add multiple activities in an Android App, we designate one activity as starting activity. By default, MainActivity is designated as starting activity using “MainLauncher = true” attribute in the activity code file.

    3-mainactivity-cs-code-to-load-view-and-set-mainlauncher-attrribute

    As far the activity lifecycle is concerned, there are many different methods which execute during the life of an activity and the activity goes through different states as follows: Activity Starts -> OnCreate() -> onStart() -> onResume() -> Activity Running -> onFreez() -> onPause() -> onStop() -> onDestroy() -> App Shuts Down.

    The commonly used methods include onCreate, onStart, onResume. In order to start working with the Android Apps, the understanding and use of onCreate method is good enough as this is the method where all the starting work is done and view is set.

    4-oncreate-method-of-mainactivity

    Hopefully, this gives you some basic idea of an Android Activity and how an activity links to a View. Also, you must have got the idea about the Android Activity lifecycle and main methods including the onCreate method. In the next post, we’ll start building our MainActivity. After that, we’ll add more activities and then navigate between those activities. This way, we’ll gradually build our MyBookstore Android App. Which can be published on Play Store or any other store, and can also be run on Android devices without even uploading to a store.

     




    Namecheap.com
    Avatar of admin

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply