Flutter app inactivity timeout

Andi
2 min readDec 6, 2020

--

A lot of mobile applications can contain sensitive user information. Such as, your bank app, your medical prescriptions app, etc. These applications are built with a lot of security features implemented in them to avoid user information leakage.

One such feature is the Inactivity Timeout feature. This allows the app to keep the information visible only for a small time and then change the state to dismiss all. This is done when the app may be active in the background.

To do this in Flutter, you only need to have access to the application lifecycle. Here’s the code to get this access:

You are concerned with the method `didChangeAppLifecycleState()`

Here, the function didChangeAppLifecycleState gives you a handle to the current state of your app. The state in this function can have values in

  1. detached
  2. inactive
  3. paused
  4. resumed

Read more about these in the docs here.

For the feature we want, we only need to understand the states paused and resumed .

Here’s how the code looks

Now that you have the handle on the app lifecycle, you need to run a counter when it is in the background.

The way I did it, was to use the quiver package. If you don’t know about it, check it out. This package has a very good set of tools to get you working out of the menial tasks.

This package also offers a CountdownTimer which makes it extremely easy to set up your timers.

If you follow this code, you now have a timer running in the background which will complete after 60 seconds. You read the state of this counter only after the app is brought back to the foreground. Remember to use the cancel() .

As always, this is the way I implemented timeout in an app. There must be other more efficient methods to do this. If you know better, please be sure to link them in the comments and I’ll update this article.

--

--

Andi
Andi

Written by Andi

Tell me what I should do next…

Responses (1)