Using a Custom Application Class in Xamarin.Android

Nathan Sokalski 4,111 Reputation points
2021-05-24T19:00:08.32+00:00

I have several variables that I want to share across all classes in my app (Activity(s), Adapter(s), ViewHolder(s), etc.). I am trying to use a custom Application by inheriting from Android.App.Application. Here is the declaration for my custom Application class:

public class ScorePadApplication : Application
{
    public static readonly int[] NameColors = new[] {  };
    public static readonly int[] ScoreColors = new[] {  };
    public static PlayerData[] Players { get; set; }
}

And here is the application line from my AndroidManifest.xml file (the only change I made is the name attribute):

<application android:name="ScorePadApplication" android:allowBackup="false" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"></application>

However, my app now refuses to do anything (it doesn't start). The only output to the Output window is the following:

am start -a "android.intent.action.MAIN" -c "android.intent.category.LAUNCHER" -n "com.companyname.scorepad/crc6420a578ac0cde9c07.MainActivity"
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.companyname.scorepad/crc6420a578ac0cde9c07.MainActivity }

Is there something else I need to do to use a custom Application class? Am I doing this wrong? Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
0 comments No comments
{count} vote

Accepted answer
  1. JarvanZhang 23,961 Reputation points
    2021-05-25T05:33:06.157+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Xamarin.Android permits writing Android apps with C# instead of Java. To create a custom Application/View class, we need to add the Java/JNI constructor.

       [Application]  
       public class CustomApplication : Application  
       {  
           protected CustomApplication(System.IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)  
           {  
           }  
           public override void OnCreate()  
           {  
               base.OnCreate();  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Graham McKechnie 421 Reputation points
    2021-05-24T23:08:08.347+00:00

    You need to add the additional protected ctor. See the Application class

    protected Application(IntPtr javaReference, JniHandleOwnership transfer);

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.