Close
Topic : Android Activity LifeCycle: MainActivity.cs - Activity States

MainActivity.cs file: infobrother

Main Activity:

The Activity Class contains the code that powers the user interface. The Activity is responsible for responding to user interaction and creating a Dynamic user Experience.



using Android.App;
using Android.Widget;
using Android.OS;

namespace App3
{
    [Activity(Label = "App3", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
        }
    }
}

Whenever we create New Android project, The project will Create a File named MainActivity.cs with some autogenerated code look like above code. This code has only one Activity (screen). The class MainActivity Power the screen and lives in the MainActivity.cs file. The Name MainActivity has no special Significance in Android, Although the concention is to name the first activity in an application MainActivity. Android does not Care, if it is named something else.

When we Open MainActivity.cs file, we can see that the MainActivity class is a subclass of the Activity class, and that the Activity is Adorned with the Activity Attribute.


namespace App3
{
    [Activity(Label = "App3", MainLauncher = true)]
    public class MainActivity : Activity
    {
      //body
    }
}

The Activity Attribute registers the Activity with the Android Manifest, this lets Android know that this class is part of the Application managed by this manifest. The Label Property sets the text that will be displayed at the top of the screen.



Activity Lifecycle:

Activities are the fundamental building block of Android Application and they can exist in a number of different states. Activity Lifecycle begins with instantiation and ends with destruction including many states in between. When an activity changes state, The appropriate lifecycle event method is called, to Notify the activity of the impending state change and allowing it to execute code to adapt to that change.

In Order to handle Android Activity Lifecycle, The Activity Class provide a collection of methods. These methods allow developers to implement the functionality that is necessary to satisfy the state and resource management requirements of their applications. The following Diagram Illustrates the states an Activity can go through during its lifetime.


Activity Lifecycle: infobrother
StatesMethodsExplanation
Create onCreate() OnCreate() is the First Method to be called when an Activity is created. ( Read More... )
Start onStart() After Finishing of onCreate(), onStart() is Always called by the system. ( Read more... )
Resumed onResume() When the Activity is Ready to start interacting with the user, The system calls onResume(). ( Read more... )
Paused onPause() When the system is about to put the activity into the background, or the user Interact with another activity, the system calls onPause() to pause the activity. ( Read more... )
Stop onStop() When the Activity is no longer visible to the user, The system call onStop() to Terminate the Activity. ( Read more... )
Destroy onDestroy() When user want to Destroy or removed or uninstall the Activity from memory, System called the Final method onDestroy(). ( Read more... )
Restart onRestart() When the Activity has been stopped, and user try to start it again, the system call onRestart() method to start the activity again. ( Read more...)




The Android Activity Lifecycle provides a Powerful framework for state management of activities within an application but it can be tricky to understand and implement. In This tutorial, we Introduced The different states that an activity may go through during its lifetime, as well as the lifecycle methods that are associated with those states. In Next tutorial, we will learn, how to use these methods to work with android activities.



















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement