Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't deal with hardware backbutton? #2271

Open
kotran88 opened this issue Sep 10, 2024 · 8 comments
Open

can't deal with hardware backbutton? #2271

kotran88 opened this issue Sep 10, 2024 · 8 comments

Comments

@kotran88
Copy link

Environment

Flutter version:

3.24.1

Plugin version:

6.0.0

Android version:

latest version

iOS version:
Xcode version:
Device information:

Description

I still can't find how to disable or work well with hardware back button on android device,
currently , when I press it, it terminate app with error below


D/SessionLifecycleService(27361): Activity backgrounding at 18032050
I/ViewRootImpl@93e145b[MainActivity](27361): handleWindowFocusChanged: 0 0 call from android.view.ViewRootImpl.-$$Nest$mhandleWindowFocusChanged:0
I/ImeFocusController(27361): onPreWindowFocus: skipped, hasWindowFocus=false mHasImeFocus=true
I/ImeFocusController(27361): onPostWindowFocus: skipped, hasWindowFocus=false mHasImeFocus=true
D/InputTransport(27361): Input channel destroyed: 'ClientS', fd=159
I/ViewRootImpl@93e145b[MainActivity](27361): handleAppVisibility mAppVisible = true visible = false
I/SurfaceView@5783d2d(27361): onWindowVisibilityChanged(8) false io.flutter.embedding.android.FlutterSurfaceView{5783d2d V.E...... ........ 0,0-840,2163} of ViewRootImpl@93e145b[MainActivity]
I/SurfaceView(27361): 91766061 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@5783d2d(27361): 91766061 Cur surface: Surface(name=null)/@0x1adef58
I/SurfaceView(27361): 91766061 surfaceDestroyed
I/SurfaceView@5783d2d(27361): surfaceDestroyed callback.size 1 #2 io.flutter.embedding.android.FlutterSurfaceView{5783d2d V.E...... ........ 0,0-840,2163}
I/SurfaceView@5783d2d(27361): updateSurface: mVisible = false mSurface.isValid() = true
I/SurfaceView@5783d2d(27361): releaseSurfaces: viewRoot = ViewRootImpl@93e145b[MainActivity]
V/SurfaceView@5783d2d(27361): Layout: x=0 y=0 w=840 h=2163, frame=Rect(0, 0 - 840, 2163)
I/ViewRootImpl@93e145b[MainActivity](27361): 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(27361): 17277644 windowPositionLost, frameNr = 0
D/OpenGLRenderer(27361): CacheManager::trimMemory(20)
I/ViewRootImpl@93e145b[MainActivity](27361): Relayout returned: old=(0,0,840,2289) new=(0,0,840,2289) relayoutAsync=false req=(840,2289)8 dur=3 res=0x402 s={false 0x0} ch=true seqId=0
I/SurfaceView@5783d2d(27361): windowStopped(true) false io.flutter.embedding.android.FlutterSurfaceView{5783d2d V.E...... ........ 0,0-840,2163} of ViewRootImpl@93e145b[MainActivity]
D/SurfaceView@5783d2d(27361): updateSurface: surface is not valid
I/SurfaceView@5783d2d(27361): releaseSurfaces: viewRoot = ViewRootImpl@93e145b[MainActivity]
D/SurfaceView@5783d2d(27361): updateSurface: surface is not valid
I/SurfaceView@5783d2d(27361): releaseSurfaces: viewRoot = ViewRootImpl@93e145b[MainActivity]
D/OpenGLRenderer(27361): CacheManager::trimMemory(40)
I/ViewRootImpl@93e145b[MainActivity](27361): stopped(true) old = false
D/ViewRootImpl@93e145b[MainActivity](27361): WindowStopped on com.onofflab.dhauto/com.onofflab.dhauto.MainActivity set to true
D/OpenGLRenderer(27361): RenderThread::destroyRenderingContext()
I/SurfaceView(27361): 91766061 Detaching SV
D/SurfaceView@5783d2d(27361): updateSurface: surface is not valid
I/SurfaceView@5783d2d(27361): releaseSurfaces: viewRoot = ViewRootImpl@93e145b[MainActivity]
I/SurfaceView@5783d2d(27361): onDetachedFromWindow: tryReleaseSurfaces()
I/SurfaceView@5783d2d(27361): releaseSurfaces: viewRoot = ViewRootImpl@93e145b[MainActivity]
D/SurfaceView@5783d2d(27361): 91766061 setAlpha: alpha=0.0
D/SurfaceView@5783d2d(27361): 91766061 updateSurface: has no frame
D/AndroidRuntime(27361): Shutting down VM
E/AndroidRuntime(27361): FATAL EXCEPTION: main
E/AndroidRuntime(27361): Process: com.onofflab.dhauto, PID: 27361
E/AndroidRuntime(27361): java.lang.RuntimeException: Unable to destroy activity {com.onofflab.dhauto/com.onofflab.dhauto.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.Lifecycle.removeObserver(androidx.lifecycle.LifecycleObserver)' on a null object reference
E/AndroidRuntime(27361):        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:6149)
E/AndroidRuntime(27361):        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:6181)
E/AndroidRuntime(27361):        at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:47)
E/AndroidRuntime(27361):        at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
E/AndroidRuntime(27361):        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:180)
E/AndroidRuntime(27361):        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:98)
E/AndroidRuntime(27361):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2685)
E/AndroidRuntime(27361):        at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(27361):        at android.os.Looper.loopOnce(Looper.java:230)
E/AndroidRuntime(27361):        at android.os.Looper.loop(Looper.java:319)
E/AndroidRuntime(27361):        at android.app.ActivityThread.main(ActivityThread.java:8919)
E/AndroidRuntime(27361):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(27361):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
E/AndroidRuntime(27361):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
E/AndroidRuntime(27361): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.Lifecycle.removeObserver(androidx.lifecycle.LifecycleObserver)' on a null object reference
E/AndroidRuntime(27361):        at com.amolg.flutterbarcodescanner.FlutterBarcodeScannerPlugin.clearPluginSetup(FlutterBarcodeScannerPlugin.java:295)
E/AndroidRuntime(27361):        at com.amolg.flutterbarcodescanner.FlutterBarcodeScannerPlugin.onDetachedFromActivity(FlutterBarcodeScannerPlugin.java:285)
E/AndroidRuntime(27361):        at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.detachFromActivity(FlutterEngineConnectionRegistry.java:381)
E/AndroidRuntime(27361):        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDetach(FlutterActivityAndFragmentDelegate.java:747)
E/AndroidRuntime(27361):        at io.flutter.embedding.android.FlutterFragment.onDetach(FlutterFragment.java:1176)
E/AndroidRuntime(27361):        at androidx.fragment.app.Fragment.performDetach(Fragment.java:3387)
E/AndroidRuntime(27361):        at androidx.fragment.app.FragmentStateManager.detach(FragmentStateManager.java:862)
E/AndroidRuntime(27361):        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:355)
E/AndroidRuntime(27361):        at androidx.fragment.app.SpecialEffectsController$FragmentStateManagerOperation.complete$fragment_release(SpecialEffectsController.kt:805)
E/AndroidRuntime(27361):        at androidx.fragment.app.SpecialEffectsController$Operation.cancel(SpecialEffectsController.kt:641)
E/AndroidRuntime(27361):        at androidx.fragment.app.SpecialEffectsController.forceCompleteAllOperations(SpecialEffectsController.kt:355)
E/AndroidRuntime(27361):        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3201)
E/AndroidRuntime(27361):        at androidx.fragment.app.FragmentManager.dispatchDestroy(FragmentManager.java:3152)
E/AndroidRuntime(27361):        at androidx.fragment.app.FragmentController.dispatchDestroy(FragmentController.java:346)
E/AndroidRuntime(27361):        at androidx.fragment.app.FragmentActivity.onDestroy(FragmentActivity.java:258)
E/AndroidRuntime(27361):        at android.app.Activity.performDestroy(Activity.java:9274)
E/AndroidRuntime(27361):        at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1491)
E/AndroidRuntime(27361):        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:6136)
E/AndroidRuntime(27361):        ... 13 more
I/Process (27361): Sending signal. PID: 27361 SIG: 9
Lost connection to device.

my code is very simple


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        print(" go back..");
        if (_canGoBack) {
          print("can go back..");
          webViewController?.goBack();
          return false;
        }
        return true;
      },
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: InAppWebView(
          initialUrlRequest: URLRequest(
            url: WebUri("http://xxx.xxx.xxx:8081"),
          ),
          onWebViewCreated: (controller) {
            webViewController = controller;
          },
          onLoadStop: (controller, url) async {
            _canGoBack = await controller.canGoBack();
          },
        ),
      ),
    );
  }
Copy link

👋 @kotran88

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!

@bnoirant
Copy link

WillPopScope is deprecated, use PopScope instead

@kotran88
Copy link
Author

kotran88 commented Sep 13, 2024

WillPopScope is deprecated, use PopScope instead

so , is it working hardware backbutton? I think remain same


@override
  Widget build(BuildContext context) {
    return PopScope(
          child: InAppWebView(
            initialUrlRequest: URLRequest(
                  url: WebUri("http://202.31.237.173:8081"),  // 웹뷰 초기 URL
                ),
            onWebViewCreated: (InAppWebViewController controller) {
              webViewController = controller;
            },
            onLoadStart: (controller, url) {
              // Optionally update UI or perform actions when page starts loading
            },
            onLoadStop: (controller, url) async {
              print("onLoadStoponLoadStop");
              bool canGoBack = await webViewController?.canGoBack() ?? false;
              print("canGoBackcanGoBackcanGoBackcanGoBack");
              print(canGoBack);
              setState(() {
                _canGoBack = canGoBack;
              });
            },
          ),
        );
  }

@bnoirant
Copy link

bnoirant commented Sep 23, 2024

There seems to be a problem on the android back button handling in recent flutter versions. I needed a quick solution so I handle back press using a flutter method called from my MainActivity onBackPressed() using https://docs.flutter.dev/platform-integration/platform-channels

@bnoirant
Copy link

WillPopScope is deprecated, use PopScope instead

so , is it working hardware backbutton? I think remain same


@override
  Widget build(BuildContext context) {
    return PopScope(
          child: InAppWebView(
            initialUrlRequest: URLRequest(
                  url: WebUri("http://202.31.237.173:8081"),  // 웹뷰 초기 URL
                ),
            onWebViewCreated: (InAppWebViewController controller) {
              webViewController = controller;
            },
            onLoadStart: (controller, url) {
              // Optionally update UI or perform actions when page starts loading
            },
            onLoadStop: (controller, url) async {
              print("onLoadStoponLoadStop");
              bool canGoBack = await webViewController?.canGoBack() ?? false;
              print("canGoBackcanGoBackcanGoBackcanGoBack");
              print(canGoBack);
              setState(() {
                _canGoBack = canGoBack;
              });
            },
          ),
        );
  }

Also please read again the documentation of PopScope

@techbuild360inc
Copy link

I couldn't find it working as well, and it is a big flaw in the Flutter WebView package itself. I have tested many no-code tools as well they are also having same issue with WebView that on tapping back either with gesture or hardware button it exits the page instead of navigating inside the web page. Hence, it would be great if you please fix this issue from the package itself?

@iqbalme
Copy link

iqbalme commented Dec 5, 2024

Do you find the solution? I also hava same problem and I still cannot use back button.

@shanelau
Copy link

shanelau commented Jan 3, 2025

Not woking on iOS. Android is ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants