退出方法有2种,本章讲一下退出的区别,以及分别什么时候使用
一、SystemNavigator.pop();
-此退出方式适用于Flutter是作为Model方式,存在于原生(例如Android)项目里面,并且,当退出时,需要通知原生项目,原生有需要处理的业务,由原生项目来处理结束整个应用(例如Android的:System.exit(0);)
Instructs the system navigator to remove this activity from the stack and return to the previous activity.
On iOS, calls to this method are ignored because of Apple's human interface guidelines state that applications should not exit themselves.
This method should be preferred over calling dart:io's exit method, as the latter may cause the underlying platform to act as if the application had crashed.
注意
- 使用此退出,原生必须自己执行结束应用,否则会报错
android.view.WindowLeaked: Activity com.test.MainActivity has leaked window DecorView@855ee8b[] that was originally added here
E/WindowManager( 7144): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:558)
E/WindowManager( 7144): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331)
E/WindowManager( 7144): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:97)
E/WindowManager( 7144): at android.app.Dialog.show(Dialog.java:538)
E/WindowManager( 7144): at android.app.Presentation.show(Presentation.java:252)
E/WindowManager( 7144): at io.flutter.plugin.platform.VirtualDisplayController.<init>(VirtualDisplayController.java:93)
E/WindowManager( 7144): at io.flutter.plugin.platform.VirtualDisplayController.create(VirtualDisplayController.java:53)
E/WindowManager( 7144): at io.flutter.plugin.platform.PlatformViewsController$1.createPlatformView(PlatformViewsController.java:105)
E/WindowManager( 7144): at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.create(PlatformViewsChannel.java:96)
E/WindowManager( 7144): at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:60)
二、exit(0);
此退出方式就适合原生项目退出时没有需要处理的业务逻辑,直接就结束应用
Flutter documentation 上关乎 exit() 是这么说的:
Exit the Dart VM process immediately with the given exit code.
This does not wait for any asynchronous operations to terminate. Using exit is therefore very likely to lose data.
使用给定的退出代码立即退出Dart虚拟机进程。
这不会等待任何异步操作终止。因此,使用exit很可能会丢失数据。
网友评论