web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to Invalidate a Se...
Power Apps
Suggested Answer

How to Invalidate a Session when a User swipes out a Mobile app screen?

(2) ShareShare
ReportReport
Posted on by 4
 
Using Exit() on timer end doesn't work in this scenario. How to resolve this ?
 
I have a mobile application where i am using Host.SessionID on app start to create a session ID unique for that particular session. 
Also, I have a logout button which when the user clicks, i am ending the session and using Exit() to come out of the application. 
 
All these events and session tracking information are being recorded in the SQL server DB. 
 
Now, if the user doesnt click the log out button, then session end time will not be recorded. So in order to close the session i have a timer which will check for invalid session activity and if the time is more than 10 minutes, then the Exit() function will be invoked and after confirmation screen user can click on close to close the app.  But this doesnt work when the user swipes away the mobile app. 
 
How to handle this scenario  to invalidate the session ?
Categories:
I have the same question (0)
  • WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    This is an often-asked question in many forms, but generally on a browser when the user closes a tab. The short answer is that you cannot detect that actual event at the data source level, nor trigger any acion from the app.
     
    You already have a timer looking for dormant sessions - you could consider tracking each activity (Form submission/Patches) by updating the date/time to a field in your data source and then look for anything more than X time after that. The session would be closed, however you can set the time ending at the last activity recorded.
     
    Happy to explore the structure of this further if the concept would suit your model.
     
    Please ✅ Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like ♥
    Visit my blog
    Practical Power Apps    LinkedIn  
     
     
  • Suggested answer
    oyepanky Profile Picture
    537 on at

    Hi,

    This behavior is expected in Power Apps mobile.

    When a user swipes away (force closes) the app, Power Apps does not trigger any event like:

    OnHidden
    OnTimerEnd
    OnSelect
    OnPause
    Exit()

    The app is simply terminated by the OS.
    So you cannot reliably invalidate a session on swipe close from inside Power Apps.

    Correct Solution (Production-Ready Approach)

    1 – Store Last Activity Time - When user logs in (App.OnStart):

    Set(varSessionID, Host.SessionID);
    
    Patch(
        SessionTable,
        Defaults(SessionTable),
        {
            SessionID: varSessionID,
            UserEmail: User().Email,
            SessionStart: Now(),
            LastActivity: Now(),
            Status: "Active"
        }
    )
    2 – Update LastActivity on Every Action - On important screens or button clicks
    Patch(
        SessionTable,
        LookUp(SessionTable, SessionID = varSessionID),
        {
            LastActivity: Now()
        }
    )

    This keeps the session alive while user is active.


    3 -Handle Expiry From SQL (NOT Power Apps) - Create logic in SQL

    UPDATE SessionTable
    SET Status = 'Expired',
        SessionEnd = GETDATE()
    WHERE Status = 'Active'
    AND DATEDIFF(MINUTE, LastActivity, GETDATE()) > 10

    You can run this via:

    • SQL Job
    • Stored Procedure
    • Power Automate scheduled flow
    This works even if user force closes app
    This works even if mobile kills process
    No dependency on Exit()

    4 – Validate Session When App Opens - In App.OnStart
    Set(
        varSessionRecord,
        LookUp(SessionTable, SessionID = Host.SessionID)
    );
    
    If(
        DateDiff(varSessionRecord.LastActivity, Now(), Minutes) > 10,
        Patch(
            SessionTable,
            varSessionRecord,
            {
                Status: "Expired",
                SessionEnd: Now()
            }
        )
    );

    If expired → force new session.

    24/7 Power Platform Support - Click Here
    Best regards,
    Pankaj Jangid (OyePanky)
    Power Platform Developer
    YouTube: https://www.youtube.com/@oyepanky
    Website: https://www.dialforit.com

  • WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    A quick follow-up to see if you received the answer you were looking for. Happy to assist further if not.
     
    Please ✅ Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like â™¥
    Visit my blog
    Practical Power Apps    LinkedIn   
    ​​​​​​
  • CU16020929-0 Profile Picture
    4 on at
    Hello oyepanky,
     
    Thank All for your response! 
    Much Appreciated. 
     
    I need one more information here,
    I have now created a Power Automate flow which will run on recurrence for every 30 minutes,
    check the records in the session table for the status as 'Active' and SessionEnd column as 'null' and if there is a record, then check the events table if there are any records for that same sessionId value, if there is one, then take last activity time of the last event performed, compare it to the present time and if the inactivity time is more than 30 minutes, then end the session and mark the status as expired.
    If for the given session id, if there are no events present in the events table, then it means user has not performed any action, in that case, i end the session and change the status as 'No action'. 
    Now this happens entirely outside of the power apps application.
    I mean I have not even included this automate flow into my power apps application. 
    So in this case, I need to understand how can this affect the session information for the user while he is still using the application in the front end ?
    Scenario1:
    Lets Say, user has done a particular transaction, then he remains inactive in the application for around 35 minutes, then as per the above settings, the automate flow will run and close that session. 
    If the user comes back to the landing screen after 36 minutes, to perform another transaction, then the session would have been closed by then. 
    Presently I have the code to create a new session id only on App.Start. 
    How to resolve this situation. ??  can I force open another session ?? Please guide me here...
  • CU16020929-0 Profile Picture
    4 on at
    In short, i just want to understand, how to check if the session is expired, and if expired, then i need to force a new session which means i should use the Exit() to make the application force close and the user will click on the app to re start a new session.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 424

#2
WarrenBelz Profile Picture

WarrenBelz 355 Most Valuable Professional

#3
11manish Profile Picture

11manish 290

Last 30 days Overall leaderboard