The usual behaviour for the app is to exit, when back button is pressed. What I want is for the app to go to background without exiting, when back button is pressed. Just like what happens when we switch from one app to another.
I know that it will involve using WillPopScope()
but how to handle the onBackPressed
event to keep the app in background, and not exit the same.
Here's how you do it: Step 1: Wrap your Scaffold widget inside the WillPopScope widget. Step 2: Inside the WillPopScope, add the onWillPop parameter and then create a new method and assign it to handle the callback. Step 3: Inside the callback, return false.
WillPopScope widget is used to detect when user press the back button. So, whenever a user press back button, you will get a callback at onWillPop function, which return Future value i.e. Either True or False. If it returned value is true, the screen will be popped and exit the .
pop() to exit the application.
Though it is pretty late to answer, but I hope it may help others as well as myself.
There's a plugin that can help in this case I think.
https://pub.dev/packages/move_to_background
It's easy to use, and has a pretty high rating.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With