App Lifecycle

My app exposes sensitive data, so anytime the user leaves the app after 'x' minutes, I want to remove their session, forcing them to log back in when they re-enter the app. This is super simple when the app is closed, and they come back in, as it calls the App constructor where I can check to see if their Session exists (I'm using CrossSettings to store this info). If it exists, I take them into the app, if it does not exist, I force them to the Login. What I can't figure out is how to force this when the OnResume() is called.

When a user comes in from the app being in the background and OnResume() is called, I check to see if it's been longer than 'x' minutes, if it has, how do I restart the app?

Best Answer

  • ChaseFlorellChaseFlorell mod CA mod
    Accepted Answer

    Can't you set your MainPage from within OnResume?

    I personally make my container static in the app, and ALWAYS private because I despise the Service Locator pattern (unless there's a super valid use case).

    public sealed partial class App
    {
        private readonly IContainer _container
    
        public App(IContainer container)
        {
            _container = container;
        }
    
        public override OnResume() {
            if(GetTimeDelta() > Settings.TimeoutInMinutes){
                MainPage = new Login(container.LoginService);
            }
            base.OnResume();
        }
    }
    

Answers

  • ChaseFlorellChaseFlorell mod CAInsider, University mod

    The way I'd try it... at least for a first run, would be to override OnSleep which is called when the app is closed (home button pressed).

    In there, you can take take a current timestamp and store it in App Settings. Then OnStart/OnResume, grab that time stamp and do a comparison.

    public sealed partial class App : Application
    {
            protected override void OnSleep()
            {
                base.OnSleep();
            }
    }
    
  • JJWilliamsJJWilliams USMember
    edited October 2015

    The timestamp comparison is working perfectly, and what you described is exactly what i'm doing. The problem is when OnResume() is called, it sees the value is greater, so it clears out the user settings I saved, but then resumes on the MainPage.

    The structure is like this:

    public App() : Application
    {
        var container = GetDependencyContainer();
    
        if(GetTimeDelta() > Settings.TimeoutInMinutes)
        {
            Settings.AuthToken = null;
        }
    
        if(AuthToken == null)
        {
            MainPage = new Login(container.LoginService)
        }
        else
        {
            MainPage = new Home(container.DocumentsService)
        }
    }
    
    public override OnResume()
    {
        // What to do here when app resumes if GetTimeDelta() > Settings.TimeoutInMinutes?
        // the ctor code above is not called, and the MainPage is loaded after this method completes
    }
    

    Note: I suppose I could duplicate all of the ctor code in the OnResume and reset the MainPage, just didn't want to re-register my dependency container if not necessary.

  • ChaseFlorellChaseFlorell mod CAInsider, University mod
    Accepted Answer

    Can't you set your MainPage from within OnResume?

    I personally make my container static in the app, and ALWAYS private because I despise the Service Locator pattern (unless there's a super valid use case).

    public sealed partial class App
    {
        private readonly IContainer _container
    
        public App(IContainer container)
        {
            _container = container;
        }
    
        public override OnResume() {
            if(GetTimeDelta() > Settings.TimeoutInMinutes){
                MainPage = new Login(container.LoginService);
            }
            base.OnResume();
        }
    }
    
  • JJWilliamsJJWilliams USMember

    That will work for me, thanks!

Sign In or Register to comment.