Forum Xamarin Xamarin.Forms

Every line in OnInitialized gets executed regardless of conditional operators

RezaMohamedRezaMohamed USMember ✭✭✭

I'm a little miffed right now, I created a simple project to test this, but can someone explain why when the app starts up it seems to execute every line in the OnInitialized method in app.xaml.cs? In my example below (project attached), my assumption is that the app will navigate to MainPage, but it seems to always navigate to ViewA. When I run the debugger and step through the code, it hits the navigation to MainPage and still goes on to ViewA. I would like the app to navigate based on the conditions on app start, but I cant seem to get it to work.

    protected override async void OnInitialized()
    {
        InitializeComponent();

        if (DateTime.Now > DateTime.MinValue)
        {
            await NavigationService.NavigateAsync("NavigationPage/MainPage");
            //return;
        }


        await NavigationService.NavigateAsync("NavigationPage/ViewA");

    }

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    From your code, we can find it's sequential statement.

       if (DateTime.Now > DateTime.MinValue)
            {
                await NavigationService.NavigateAsync("NavigationPage/MainPage");
                //return;
            }
          await NavigationService.NavigateAsync("NavigationPage/ViewA");
    

    If you want to navigate based on the conditions on app start, you can add else condition.

          if (DateTime.Now > DateTime.MinValue)
                {
                    await NavigationService.NavigateAsync("NavigationPage/MainPage");
                    //return;
                } else { 
                    await NavigationService.NavigateAsync("NavigationPage/ViewA");
                }
    
  • RezaMohamedRezaMohamed USMember ✭✭✭
    edited March 31

    So in my production app I have the following conditions, but on startup when I set a breakpoint at the ViewA await it keeps hitting it even though the conditions in the top IF are true and hit LoginPage first.

            if (sessionExpiry < DateTime.Now && sessionExpiry != DateTime.MinValue)
            {
                if (string.IsNullOrEmpty(userName))
                {
                    await NavigationService.NavigateAsync("/LoginPage");
                }
    
                if (!isFacebookLogin)
                {
                    if (string.IsNullOrEmpty(password))
                    {
                        await NavigationService.NavigateAsync("/LoginPage");
                    }
                }
            }
    
        // there are more lines of code here, but below is the second await that gets hit 
                if (String.IsNullOrEmpty(clientId) || clientId == "0")
                {
                    await NavigationService.NavigateAsync("/MenuPage/NavigationPage/ViewA");
                }
                else
                {
                    await NavigationService.NavigateAsync("/MenuPage/NavigationPage/ViewB");
                }
    

    Essentially what I need is a way to execute everything in the bottom if any of the cases in the IF and nested IFs fail - this is where I am stuck.

  • jezhjezh Member, Xamarin Team Xamurai
    edited March 31

    if (String.IsNullOrEmpty(clientId) || clientId == "0")

    One of the problems with your code is clientId == "0", if we want to compare two strings, we should use function Equals, so the code is like this:
    clientId.Equals("0").

       if (String.IsNullOrEmpty(clientId) ||  clientId.Equals("0"))
            {
                await NavigationService.NavigateAsync("/MenuPage/NavigationPage/ViewA");
            }
            else
            {
                await NavigationService.NavigateAsync("/MenuPage/NavigationPage/ViewB");
            }
    

    Note

    I don't know the special value of these variables, you can debug step by step to check their values,then set up the judgment logic according to your requirements.

  • RezaMohamedRezaMohamed USMember ✭✭✭

    It is an OR condition. clientId is getting stored in SecureStorage. The API returns a default(int) which is 0 if there is no client. Checking for NullOrEmpty because the first time the app is executed SecureStorage is not created/on Logout SecureStorage.Remove will delete it, so it's null.

  • jezhjezh Member, Xamarin Team Xamurai

    Hey friend @RezaMohamed

    The API returns a default(int) which is 0 if there is no client.

    According to your description, the type of clientId is int or string?

    Besides, can you list what values clientId might have, and then we can set the criteria based on those values

  • RezaMohamedRezaMohamed USMember ✭✭✭

    clientId is a string, it is from SecureStorage. so:
    var clientId = await SecureStorage.GetAsync("ClientIdentifier");

    what I meant when I said int was that I am populating securestorage with data from an API, that API is sending either a client Id or default(int) which is 0. When its stored to SecureStorage, its converted to string.

  • jezhjezh Member, Xamarin Team Xamurai
    edited March 31

    The question is do you have anything to filter other than those two conditions(String.IsNullOrEmpty(clientId) || clientId.Equals("0") ).

    The next step is to add filters based on your own logic.
    Please feel free to touch us if you have any other question. :)

  • RezaMohamedRezaMohamed USMember ✭✭✭
    edited April 10

    Does OnInitialized() in XF get executed every time the apps starts, restarts (assume user was in the app, presses home button, and later starts the app again) and also finally if the app was force closed (in ios double home, swipe up) and restarted?

    Ive read up on the Activity life cycle, but the OnStart, OnResume and OnSleep is unclear. My assumption is that OnStart gets executed only when the app cold starts, and OnResume only when the App is closed and reawakened. For reference I am using PRISM.

Sign In or Register to comment.