locked
Activity vs Application RRS feed

  • Question

  • User141496 posted

    Whats the difference b/w using Activity vs Application in Xamarin? Why would one want to use one approach over another?

    For example, when I create a new Xamarin Form project in VS I get the follow:

    [Activity(Label = "eApprove Main Activity", MainLauncher = true, Icon = "@drawable/eA_icon")]
    public class MainActivity : Activity
    {
                ...
    }
    

    I have also seen examples on internet where people are using Application class as their main Entry point:

    namespace MyExpenses.Android
    {
           [Application(Theme = "@android:style/Theme.Holo.Light") ]
           public class MyExpensesApplication : Application
           {
                   public static Activity CurrentActivity { get; set; }
    
                   public MyExpensesApplication(IntPtr handle, global::Android.Runtime.JniHandleOwnership transer)
                      :base(handle, transer)
                  {
                  }
    
                  public override void OnCreate()
                  {
                      base.OnCreate();
                  }
           }
    }
    
    Sunday, August 9, 2015 1:39 AM

Answers

  • User48032 posted

    In an Android application, you will notice that the AndroidManifest.xml file contains an entry for which is used to configure various properties of your application: icon, label, theme, etc. Depending on your application's needs, this is sufficient. The application when instantiated will perform the necessary initializations based on the values configured in the manifest, and it will then load the Activity that is defined as the view to load on startup.

    In some cases, you may wish to extend the Application class in order to perform custom initialization on application startup, or to maintain global application state (for example, in the case of a game that is tracking score across multiple levels). The example that you have shown above is the case where someone has decided to subclass the Application class and override it's OnCreate method, although they haven't included any custom code in the override method. It looks like their purpose was to create a global static property to always have access to the Activity that is currently loaded.

    An Activity is a screen in your application that provides a UI the user can interact with. Your application may have one or more Activities. Your application must have one Activity defined as the one that is launched on startup (i.e. the default page/view that the user is presented with). Notice in your example, this is the attribute where MainLauncher=true.

    You can also create Fragments which are in essence reusable lightweight views which you can use within your Activity (think of them like User Controls if you're from a Windows development background).

    Here are some links to the Android documentation which you may find helpful: http://developer.android.com/guide/topics/manifest/application-element.html http://developer.android.com/reference/android/app/Application.html http://developer.android.com/reference/android/app/Activity.html http://developer.android.com/guide/components/fragments.html

    This post would be more suited in the Android thread, since it is not specific to Xamarin.Forms.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, August 9, 2015 3:43 AM
  • User181 posted

    This is more of an Android question. For every app the real entry point is an Application instance. However, there is a default implementation that most Android apps uses. Some need custom behavior so they create custom subclasses like you see in your quoted code.

    An activity is a class that represents one screen in an Android application. It is a reusable piece of UI with its own life cycle, but it is always loaded in a process that has an Application instance as well. There can be multiple activities in an app, but just one Application instance. One activity is marked as the "main" activity, and that activity is launched when the app launches. That's why some people think of it as the entry point.

    There is also a Forms Application class, which you are expected to subclass in a PCL. In a Forms app for Android you only have to create a main activity (inherited from FormsApplicationActivity) and in its OnCreate you create the Forms Application subclass instance and tell the base class to use it by calling LoadApplication. That code is just boilerplate (included in the template), and for the most part you can think of the entry point for a Forms application to be your Forms Application sub class's constructor or OnStart method.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, August 9, 2015 3:49 AM

All replies

  • User48032 posted

    In an Android application, you will notice that the AndroidManifest.xml file contains an entry for which is used to configure various properties of your application: icon, label, theme, etc. Depending on your application's needs, this is sufficient. The application when instantiated will perform the necessary initializations based on the values configured in the manifest, and it will then load the Activity that is defined as the view to load on startup.

    In some cases, you may wish to extend the Application class in order to perform custom initialization on application startup, or to maintain global application state (for example, in the case of a game that is tracking score across multiple levels). The example that you have shown above is the case where someone has decided to subclass the Application class and override it's OnCreate method, although they haven't included any custom code in the override method. It looks like their purpose was to create a global static property to always have access to the Activity that is currently loaded.

    An Activity is a screen in your application that provides a UI the user can interact with. Your application may have one or more Activities. Your application must have one Activity defined as the one that is launched on startup (i.e. the default page/view that the user is presented with). Notice in your example, this is the attribute where MainLauncher=true.

    You can also create Fragments which are in essence reusable lightweight views which you can use within your Activity (think of them like User Controls if you're from a Windows development background).

    Here are some links to the Android documentation which you may find helpful: http://developer.android.com/guide/topics/manifest/application-element.html http://developer.android.com/reference/android/app/Application.html http://developer.android.com/reference/android/app/Activity.html http://developer.android.com/guide/components/fragments.html

    This post would be more suited in the Android thread, since it is not specific to Xamarin.Forms.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, August 9, 2015 3:43 AM
  • User181 posted

    This is more of an Android question. For every app the real entry point is an Application instance. However, there is a default implementation that most Android apps uses. Some need custom behavior so they create custom subclasses like you see in your quoted code.

    An activity is a class that represents one screen in an Android application. It is a reusable piece of UI with its own life cycle, but it is always loaded in a process that has an Application instance as well. There can be multiple activities in an app, but just one Application instance. One activity is marked as the "main" activity, and that activity is launched when the app launches. That's why some people think of it as the entry point.

    There is also a Forms Application class, which you are expected to subclass in a PCL. In a Forms app for Android you only have to create a main activity (inherited from FormsApplicationActivity) and in its OnCreate you create the Forms Application subclass instance and tell the base class to use it by calling LoadApplication. That code is just boilerplate (included in the template), and for the most part you can think of the entry point for a Forms application to be your Forms Application sub class's constructor or OnStart method.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, August 9, 2015 3:49 AM