当我点击手机上的后退按钮时,我的应用程序将关闭

问题描述 投票:0回答:1

当我点击手机上的后退按钮时,应用程序将关闭。我希望它回到之前的路线而不是退出。

这也不起作用:

android:enableOnBackInvokedCallback="true"

在我的应用程序中有两种类型的用户。每个用户的路线等由 2 个主屏幕处理。我在我的应用程序中使用

navigatior.push
Navigator.of(context).pop()

我尝试了

android:enableOnBackInvokedCallback="true"
没有成功。

此外

WillPopScope
已被弃用。即使没有,如何使用?

请帮忙

这是我的日志:

I/ViewRootImpl@409734a[MainActivity](20725): ViewPostIme pointer 0
I/ViewRootImpl@409734a[MainActivity](20725): ViewPostIme pointer 1
I/ViewRootImpl@409734a[MainActivity](20725): handleWindowFocusChanged: 0 0 call from android.view.ViewRootImpl.-$$Nest$mhandleWindowFocusChanged:0
I/ImeFocusController(20725): onPreWindowFocus: skipped, hasWindowFocus=false mHasImeFocus=true
I/ImeFocusController(20725): onPostWindowFocus: skipped, hasWindowFocus=false mHasImeFocus=true
D/InputTransport(20725): Input channel destroyed: 'ClientS', fd=197
D/InputTransport(20725): Input channel destroyed: 'ClientS', fd=216
I/ViewRootImpl@409734a[MainActivity](20725): handleAppVisibility mAppVisible = true visible = false
I/SurfaceView@de200a8(20725): onWindowVisibilityChanged(8) false io.flutter.embedding.android.FlutterSurfaceView{de200a8 V.E...... ........ 0,0-1080,2196} of ViewRootImpl@409734a[MainActivity]
I/SurfaceView(20725): 232915112 Changes: creating=false format=false size=false visible=true alpha=false hint=false visible=true left=false top=false z=false attached=true lifecycleStrategy=false
I/SurfaceView@de200a8(20725): 232915112 Cur surface: Surface(name=null)/@0x862e11c
I/SurfaceView(20725): 232915112 surfaceDestroyed
I/SurfaceView@de200a8(20725): surfaceDestroyed callback.size 1 #2 io.flutter.embedding.android.FlutterSurfaceView{de200a8 V.E...... ........ 0,0-1080,2196}
I/SurfaceView@de200a8(20725): updateSurface: mVisible = false mSurface.isValid() = true
I/SurfaceView@de200a8(20725): releaseSurfaces: viewRoot = ViewRootImpl@409734a[MainActivity]
V/SurfaceView@de200a8(20725): Layout: x=0 y=0 w=1080 h=2196, frame=Rect(0, 0 - 1080, 2196)
I/ViewRootImpl@409734a[MainActivity](20725): destroyHardwareResources: Callers=android.view.ViewRootImpl.performTraversals:3932 android.view.ViewRootImpl.doTraversal:3288 android.view.ViewRootImpl$TraversalRunnable.run:11344 android.view.Choreographer$CallbackRecord.run:1689 android.view.Choreographer$CallbackRecord.run:1698 android.view.Choreographer.doCallbacks:1153 android.view.Choreographer.doFrame:1079 android.view.Choreographer$FrameDisplayEventReceiver.run:1646 android.os.Handler.handleCallback:958 android.os.Handler.dispatchMessage:99
D/SurfaceView(20725): 141886979 windowPositionLost, frameNr = 0
D/OpenGLRenderer(20725): CacheManager::trimMemory(20)
I/ViewRootImpl@409734a[MainActivity](20725): Relayout returned: old=(0,0,1080,2340) new=(0,0,1080,2340) relayoutAsync=false req=(1080,2340)8 dur=7 res=0x402 s={false 0x0} ch=true seqId=0
I/SurfaceView@de200a8(20725): windowStopped(true) false io.flutter.embedding.android.FlutterSurfaceView{de200a8 V.E...... ........ 0,0-1080,2196} of ViewRootImpl@409734a[MainActivity]
D/SurfaceView@de200a8(20725): updateSurface: surface is not valid
I/SurfaceView@de200a8(20725): releaseSurfaces: viewRoot = ViewRootImpl@409734a[MainActivity]
D/SurfaceView@de200a8(20725): updateSurface: surface is not valid
I/SurfaceView@de200a8(20725): releaseSurfaces: viewRoot = ViewRootImpl@409734a[MainActivity]
D/OpenGLRenderer(20725): CacheManager::trimMemory(40)
D/OpenGLRenderer(20725): RenderThread::destroyRenderingContext()
I/ViewRootImpl@409734a[MainActivity](20725): stopped(true) old = false
D/ViewRootImpl@409734a[MainActivity](20725): WindowStopped on com.calistim.calistimapp/com.calistim.calistimapp.MainActivity set to true
D/SurfaceView@de200a8(20725): updateSurface: surface is not valid
I/SurfaceView@de200a8(20725): releaseSurfaces: viewRoot = ViewRootImpl@409734a[MainActivity]
D/SurfaceView@de200a8(20725): 232915112 setAlpha: alpha=0.0
D/SurfaceView@de200a8(20725): updateSurface: surface is not valid
I/SurfaceView@de200a8(20725): releaseSurfaces: viewRoot = ViewRootImpl@409734a[MainActivity]
W/WindowOnBackDispatcher(20725): sendCancelIfRunning: isInProgress=falsecallback=io.flutter.embedding.android.FlutterActivity$1@3084a9b
I/SurfaceView(20725): 232915112 Detaching SV
D/SurfaceView@de200a8(20725): updateSurface: surface is not valid
I/SurfaceView@de200a8(20725): releaseSurfaces: viewRoot = ViewRootImpl@409734a[MainActivity]
I/SurfaceView@de200a8(20725): onDetachedFromWindow: tryReleaseSurfaces()
I/SurfaceView@de200a8(20725): releaseSurfaces: viewRoot = ViewRootImpl@409734a[MainActivity]
I/ViewRootImpl@409734a[MainActivity](20725): dispatchDetachedFromWindow
D/InputTransport(20725): Input channel destroyed: '3546320', fd=142
I/FA      (20725): Application backgrounded at: timestamp_millis: 1728395452012
flutter flutter-routes
1个回答
0
投票

Flutter v3.12.0-1.0.pre 之后,

WillPopScope
已弃用。您应该使用
PopScope
来代替。下面是如何实现它:

return PopScope(
  canPop: true, // Blocks the route from being popped if set to false
  onPopInvoked: (didPop) {
    // Your logic here
    setStatusBarColor(
      statusBarColorPrimary,
      statusBarIconBrightness: Brightness.light,
    );
    finish(context);
  },
  child: Scaffold(
    // Your UI components here
  ),
);

PopScope
现在是 Flutter 3.12+ 中处理后退按钮行为的首选方式。

© www.soinside.com 2019 - 2024. All rights reserved.