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.
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