0

i want try implementation AppLifecycleState using flutter.

I have Model like this :

  • (string) id
  • (string) name
  • (bool) fingerprintStatus = false
  • (bool) tokenExpiry = false

I want detect If the user exit From the App , If the user exit more than X Second i want change status tokenExpiry == true . So for handle my case i detect user Activity Using AppLifecycleState and Timer

But the problem is i don't know logic to do that. In my mind If user exit/close app , running timer for X second then update tokenExpiry . If user comeback again but the Timer not finished, Cancel and Reset Timer then do nothing.

How can i do this ?

My Expected

AppLifecycleState _appLifecycleState;

@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
 print(state.toString());
if(_appLifecycleState == AppLifecycleState.inactive || _appLifecycleState == 
 AppLifecycleState.paused){
 => Running The Timer For 10 Second
 }
else{
  => Cancel The Timer and Do Nothing
}
    super.didChangeAppLifecycleState(state);
  }
Zeffry Reynando
  • 3,445
  • 12
  • 49
  • 89
  • This question has an odd premise. Can you please explain what you are trying to achieve as the end-goal? It will allow us to help you come up with a solution. – J. S. Dec 23 '19 at 11:12
  • I already make it clear. – Zeffry Reynando Dec 23 '19 at 11:29
  • If the app is suspended in the background of the phone you can't guarantee that your Timer would be running. What you could do is save the current Timestamp every time your user interacts with the app and then when the user comes back compare the current Timestamp with the one saved. There is a StackOverflow answer that explains how to check the app lifecycle: https://stackoverflow.com/questions/49869873/flutter-update-widgets-on-resume – J. S. Dec 23 '19 at 11:39
  • I not consider if Timer can't running if the App close, So what is best practice about my case ? because in my mind , i only have solution like my question above. – Zeffry Reynando Dec 23 '19 at 12:00
  • can you give advice about my solution ? So i should have 2 more variable in my Model **Time exit** & **Time in**. I should save Time user Exit the App then if the user comeback i save Time User In into App. After that i compare if Time In > Time out + **X** second Then update tokenExpiry. What do you think ? – Zeffry Reynando Dec 23 '19 at 12:19
  • A simple solution would be as a I said on my comment above. Save a Timestamp into SharedPreferences (for example) and then when the app lifecycle resumes you retrieve that Timestamp and compare it with the current time. – J. S. Dec 23 '19 at 12:23
  • I still ambiguous, what do you mean about Timestamp? It's time of user exit the app? Or what? But I get the point as you said about compare with current time – Zeffry Reynando Dec 23 '19 at 12:36

1 Answers1

0

I know this is coming late but the best approach will be to save a Timestamp into SharedPreferences and then when the app AppLifecycleState resumes you retrieve that Timestamp and compare it with the current time AppLifecycleState Resumes.

Feshibaba
  • 89
  • 1
  • 6