Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] xamarin.forms – How to make the Android Project of my Xamarin app start with MainApplication.cs instead of MainActivity.cs

Posted on November 11, 2022 By

Solution 1 :

In your AssemblyInfo.cs remove this:

#if DEBUG
[assembly: Application(Debuggable=true)]
#else
[assembly: Application(Debuggable = false)]
#endif

Then add it to your MainApplication like this:

#if DEBUG
[Application(Debuggable = true)]
#else
[Application(Debuggable = false)]
#endif
   public class MainApplication : global::Android.App.Application
   {      
      public MainApplication(IntPtr handle, JniHandleOwnership transer): base(handle, 
        transer)
      {
      }

      public override void OnCreate()
      {
        base.OnCreate();
        CrossCurrentActivity.Current.Init(this);

        //A great place to initialize Xamarin.Insights and Dependency Services! Add 
         some function that you want.
      }

   }

Solution 2 :

And ideas on how I can refactor the Android Project to begin with a MainApplication instead of MainActivity?

According to the link that you provided, if you want to Android project start with MainApplication, you just create new class name MainApplication.cs in Android project, if you want to perform some functions when Android project starts, you just add some code that you want in OnCreate() method.

#if DEBUG
[Application(Debuggable = true)]
#else
[Application(Debuggable = false)]
#endif
public class MainApplication : global::Android.App.Application
{      
    public MainApplication(IntPtr handle, JniHandleOwnership transer): base(handle, transer)
    {
    }
    public override void OnCreate()
    {
        base.OnCreate();
        CrossCurrentActivity.Current.Init(this);

        //A great place to initialize Xamarin.Insights and Dependency Services! Add some function that you want.
    }

}

You can see when you run project,enter MainApplication firstly, then Mainactivity,I think the article that you provided, has solved your question.

Problem :

I am working on a Xamarin.Forms application that has Xamarin.Android and Xamarin.ios projects. I need to use the CurrentActivityPlugin for Android.

I need to perform some functionality when the Android app starts, hence I want to my app to begin with a MainApplicaiton instead of a MainActivity as shown in the docs here.

And ideas on how I can refactor the Android Project to begin with a MainApplication instead of MainActivity?

Comments

Comment posted by Kikanye

Yh I know to do this, but when I simply add this file with the code above I get a build error. Seems there is more to it than just adding this file, Maybe some way to specifically indicate that this is the starting point of your app, and begin the main Activity from here or something.

READ  [FIXED] android - How can I achieve ripple or wave or radar loading like the below layout in jetpack compose
Powered by Inline Related Posts

Comment posted by github.com/CherryBu/testapp.An

@Kikanye What is error message? I don’t have any issue when register Application, this is my sample:

Comment posted by Kikanye

The error was a System.InvalidOperationException which said Application cannot have both a type with an [Application] attribute and an [assembly:Application] attribute

Android Tags:xamarin, xamarin.android, xamarin.forms

Post navigation

Previous Post: [FIXED] android – Convert BMP image to PNG or JPG in Java without loading the entire image in memory
Next Post: [FIXED] xml – Android – prevent TextView to push out another element inside LinearLayout

Related Posts

[FIXED] How to Clear the RealmResults<> of a particular Query while Filtering through the Realm in Android? Android
[FIXED] Android Studio Fails with: “File was deleted, but previous version not found in cache” Android
[FIXED] xamarin.forms – Xamarin From List, Navigate to details and back from details to list Android
[FIXED] android – Problem with build version when publishing APK to Play Store Android
[FIXED] kotlin – Android – Activity/fragment lifecycle event when user closes app or OS closes app Android
[FIXED] xamarin.android – How to initialize Nfc Tag if tag is empty and make it password protected in Xamarin Android? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme