OnCreate(..) function of the MainActivity class never called

Hi,
i create a new crossplatform xamarin Solution in Visual Studio 2017.
blank app | Android and IOS platform | Xamarin.Forms Technologie | Shared Project

I compile and set the android project as starting project.
I am pairing my phone with the Live Player to debug on my Android phone.
When i run the app in Debug mode, the app starts and the label "Welcome to Xamarin.Forms!" appears.

Everything looks fine, but why is the OnCreate(..) function of the MainActivity class never called?

Please help!

Answers

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @camelord said:
    Hi,
    i create a new crossplatform xamarin Solution in Visual Studio 2017.
    blank app | Android and IOS platform | Xamarin.Forms Technologie | Shared Project

    I compile and set the android project as starting project.
    I am pairing my phone with the Live Player to debug on my Android phone.
    When i run the app in Debug mode, the app starts and the label "Welcome to Xamarin.Forms!" appears.

    Everything looks fine, but why is the OnCreate(..) function of the MainActivity class never called?

    Please help!

    Have you set "MainLauncher = true" on MainActivity ?

  • camelordcamelord Member ✭✭

    Yes - the visual studio project template set it correctly.
    Should i create a new solution and attach it somehow here?

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @camelord said:
    Yes - the visual studio project template set it correctly.
    Should i create a new solution and attach it somehow here?

    without Firing Oncreate its highly impossible to Display "Welcome to Xamarin Forms" ,bcs that is intilization point, i think maybe breakpoint not hitting.

  • JohnHardmanJohnHardman GBUniversity mod

    @camelord - Use the old school debugging technique of

    System.Diagnostics.Debug.WriteLine("In OnCreate");
    

    Put that in your OnCreate method and see whether you see "In OnCreate" appear in your VS Output window.

  • camelordcamelord Member ✭✭

    I set a breakpoint into the onCreate() function block, that is never called.
    So yes - this is why i am claiming that it is never called.
    Other breakpoints hit like the constructor of MainPage()..

    I tried to debug by wire (USB) and the breakpoint hit :o

    Is there a Bug with the Live Player or did i missed any settings compared to the defaults?

  • JohnHardmanJohnHardman GBUniversity mod
    edited January 2018

    @camelord - There have been many reports of breakpoints not being triggered when developing using Xamarin.Forms, even though the code with the breakpoint is executed. So it's worth double-checking using the old-school method if in doubt.

    Regarding OnCreate, which signature are you using?

    void OnCreate(Bundle bundle, PersistableBundle persistentState)
    

    or:

    void OnCreate(Bundle savedInstanceState);
    

    Is your MainActivity derived from FormsAppCompatActivity ?

  • camelordcamelord Member ✭✭
    [Activity(Label = "Camera", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                TabLayoutResource = Resource.Layout.Tabbar;
                ToolbarResource = Resource.Layout.Toolbar;
    
                base.OnCreate(bundle);
    
                global::Xamarin.Forms.Forms.Init(this, bundle);
                LoadApplication(new App());
            }
      }
    

    This is the generated onCreate Function

  • JohnHardmanJohnHardman GBUniversity mod
    edited January 2018

    @camelord - Have you tried the System.Diagnostics.Debug.WriteLine in OnCreate to see if it is executed in the scenario where the breakpoint is not triggered?

  • I concur that IDE breakpoints sometimes don't fire. I've found that manually setting the breakpoint can work as an alternative as well: System.Diagnostics.Debugger.Break();

  • camelordcamelord Member ✭✭

    damn - it doesnt work! I get no Debug log..
    Is it possible that the theme or style is a problem?

  • camelordcamelord Member ✭✭

    Ok i quit trying.. with wired debugging everything works (breakpoint in OnCreate) - with Live Player, debugging is impossible :(

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @camelord said:
    Ok i quit trying.. with wired debugging everything works (breakpoint in OnCreate) - with Live Player, debugging is impossible :(

    Why dont you try creating a sample project and check once ?

  • camelordcamelord Member ✭✭
    Check once? You mean try again?
    I tried all your good proposals and some from the Internet, without luck..
  • I have the exact same issue. In fact, I can comment out the entire MainActivity.cs and still program runs in Xamarin Live even after rebuild. MainPage.xaml.cs in UWP hits and debug break points works fine.

Sign In or Register to comment.