Is there any method similar to this.finish()
in android to finish current flutter activity.
3 Answers
Do you mean close current screen and come back to previous screen? If that, Navigator.pop(context) should do the work. However, if you are at the entry screen (the first screen of the app and this screen has no parent screen/previous screen), Navigator.pop(context) will return you to a black screen. In this case, we have to use SystemNavigator.pop().
But don't use SystemNavigator.pop() for iOS, Apple says that the application should not exit itself :)
Below code will work on Android for both cases
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
-
-
1I got the solution view this so post, stackoverflow.com/questions/50929083/…– JayeshJun 20, 2018 at 6:57
-
This works for me thanks. But can you provide a solution for iOS instead of just saying don't exit ? Oct 11, 2020 at 18:20
-
Hi, Apple will not approve us to exit the app. As all iOS apps you may use, you can not exit it from the app. Oct 12, 2020 at 10:53
use Navigator.pushReplacement(BuildContext context, Route<T> newRoute)
to open a new route which replace the current route of the navigator
-
this is not working in flutter 2.2... atleast in my code? The activity closes but the data done on the activity remains constant– heyomJan 1, 2022 at 3:13
use thie code
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) {
return AddDemoUnitActivity();
}));