Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter: How to not exit the app on back button press

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.

like image 681
Ayush Shekhar Avatar asked Jun 15 '19 01:06

Ayush Shekhar


People also ask

How do you lock the back button in Flutter?

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.

Will pop scope to exit app Flutter?

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 .

How do I exit the Flutter app on button click?

pop() to exit the application.


1 Answers

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.

like image 121
Mohammad Nazmul Hasan Avatar answered Oct 29 '22 03:10

Mohammad Nazmul Hasan