Jim Simon
Jim Simon

Reputation: 388

How to make my flutter app return the user to the OS home screen?

I'm working on an app that will have a lock screen, but I'm having some issues getting it to behave correctly. Right now I'm using the didChangeAppLifecycleState to navigate to the lock screen when the user suspends/resumes the app. I'm also using the WillPopScope to intercept and deny the back button. The problem with this approach is that pressing the back button and having nothing happen doesn't feel super intuitive. Ideally, I'd like the back button to take the user out of the app and back to their OS home screen when they're on the lock screen. I also want to do this in a way that the user's route history is maintained for when they successfully unlock the app.

Is there any way to accomplish what I'm trying to do, or should I just accept that the back button won't do anything on the lock screen?

Upvotes: 0

Views: 478

Answers (1)

westdabestdb
westdabestdb

Reputation: 4648

You can create an identifier in your LockScreen state and check for the identifier in onWillPop and if the user is pressing the back button from the lock screen, exit the app.

String identifier = "lockscreen";

bool onWillPop() {
  if (identifier == "lockscreen") {
    SystemNavigator.pop();
    SystemChannels.platform.invokeMethod('SystemNavigator.pop'); //preferred.*
    return true;
  }
}

SystemNavigator.pop(): On iOS, calls to this method are ignored because Apple's human interface guidelines state that applications should not exit themselves.

Upvotes: 1

Related Questions