1
votes

I'm pretty new to xamarin / android development but this is pretty much what I'm trying conceptually:

  • Have a main activity that checks whether or not a user is already logged in
  • If the user is logged in, start the MainAppActivity
  • If the user is not logged in, start the OnboardingActivity

The reason I think these should be in different activities is for general design reasons (separation of concerns etc) - but also because both activities have different themes. (The Onboarding process has no title bar)

In code, this should be pretty much what I need:

[Activity(Label = "MyApp", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.Black.NoTitleBar")]
public class MainActivity : Activity
{
    private readonly TransactionService _transactionService;

    public MainActivity()
    {
        var isLoggedIn = true; // This will be loaded from somewhere

        if(isLoggedIn)
        {
            var intent = new Intent(this, typeof(MainAppActivity));
            StartActivity(intent);
        } 
        else
        {
            var intent = new Intent(this, typeof(OnboardingActivity));
            StartActivity(intent);
        }
    }
}

However, this code just throws an unspecified exception.

If I try the same thing during the protected override void OnCreate(Bundle bundle) process, I get an exception as well.

However, when I bind something like that to a button event and click it manually, it will work. (That is not my intent, but to test there is nothing wrong with my activities)

So, question is, how do I do something like this?

Edit: Here is the stacktrace if I try it in the constructor:

0x29 in System.Diagnostics.Debugger.Mono_UnhandledException_internal    C#
0x1 in System.Diagnostics.Debugger.Mono_UnhandledException at /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Diagnostics/Debugger.cs:122,4 C#
0x6 in Android.Runtime.UncaughtExceptionHandler.UncaughtException at /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Runtime/UncaughtExceptionHandler.cs:35,4 C#
0x1C in Java.Lang.Thread.IUncaughtExceptionHandlerInvoker.n_UncaughtException_Ljava_lang_Thread_Ljava_lang_Throwable_ at /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Java.Lang.Thread.cs:221,5 C#
0x1D in object.b8bd1d31-3e2e-454d-bd94-9d5dea40eddb C#
1
Could you include a logcat capture of the events leading up to the crash? It should include more information than just the crashes stack-trace. - matthewrdev

1 Answers

1
votes

I have not used xamarin, so I could be wrong.

FYR, on native android, you should do that in the onCreate of your MainActivity instead of Constructor