[SOLVED] Android 9 – Back button not working

Hello everybody.

I am working on an application with Fraework7-V3 and Cordova. I need your help because in Android 9 the physical button "backbutton" does not work while in other devices with different versions of Android it works correctly.

In Android 9, when pressing the physical button "backbutton" the onBackKeyDown does not trigger, however the application is closed. It only starts working correctly if, for example, the keyboard is displayed or after locking / unlocking the device.

What is very confusing is that in other versions of the Operating System the button works correctly.

Also, when debugging I have not detected any error.

Another fact to keep in mind is that if you do not install Framework7 in the app, the onBackKeyDown fires correctly.

Environment details:

  • Framework7 version: 3.6.0
  • Cordova version: 8.1.2 ([email protected])
  • Cordova-android version: 7.1.4
  • Android Studio: 3.3

Part of the JS code of the Application:

    var $$ = Dom7;
    var myApp = new Framework7({
    root: '#app', 
    id: 'com.rma.testbackbtn', 
    name: 'Test Back Btn',
    init: false,
    theme: 'auto',
    panel: {
        swipe: 'left'
    },
    view: {
        stackPages: true,
        animateWithJS: true
    },
    navbar: {
        scrollTopOnTitleClick: true,
    },
        routes: routes
    });

    var mainView = myApp.views.create('#view-home', {
        url: '/',
        stackPages: true,
        main: true
    });

    $(document).ready(function() 
    {
        myApp.init();
    });

    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() 
    {
       document.addEventListener("backbutton", onBackKeyDown, false);
    }

    function onBackKeyDown () 
    {
    	console.log('Back button event');
        var currentPage = mainView.router.currentRoute.name;

    	if (currentPage == 'page-home')
        {
            navigator.app.exitApp();
        } 
        else 
        {
    	    mainView.router.back();
        }
    }

Workaround in V1:
I have had exactly the same bug but with version 1 of Framework7. For this case I found a workaround in which I set the fastClick to false and after the first touch of the user the onBackKeyDown is triggered correctly. In Framwork7 V3 this didn’t work.

Somebody could help me?

Thank you very much.

Sorry for my mistake, the bug was related to the plugin “cordova-plugin-splashscreen” and has been fixed.

To see the details: https://github.com/apache/cordova-plugin-splashscreen/issues/186

2 Likes

Back button event not firing. watch video for solution worked for me.