Monitor the return key of an Android device from a Web page

Source: Internet
Author: User

When recently engaged in the Android project, encountered a need to compare the egg ache, need to call the system browser from the client app to open a page, after some assorted interaction, return to the app. How to open a browser and how to return to the app here is not to say, interested children's shoes can communicate privately.

The reason for this requirement is that the egg hurts because Android has a physical return key ... Return key Ah ... Key Ah ... Ah ...

When the user presses the back key, it is expected to follow the back button on the page-back to the app. However, this return key is pressed when the page does not know AH (onbeforeunload does not count), can not find a direct way to monitor, sorrow to death we this whole bitter force programmer bird ~

Although AH can not be directly monitored, the method of saving the curve, or there are drops.

After arduous searching, we found that using HTML5 's history can slightly simulate the press event of the return key. The principle is as follows:

    1. When the page load is complete, the call History.pushstate writes a state of the specified status and listens for window.onpopstate;
    2. When the onpopstate is triggered, Check if Event.state equals state, if equal, indicates that the page has been back (by pressing the return key or the browser's Back button), then this behavior as a return key was pressed (the click-Browser Back button also mistakenly counted in, but there is no good way to do it).

Well, in order to facilitate the invocation, the logic is slightly encapsulated below, the code is shown here (Https://github.com/iazrael/xback), using the following methods:

Xback.listen (function () {    alert (' oh! Press the Back button '); Xback.listen (function () {    alert (' Ah, press press press ');

But this method has some drawbacks:

    1. If the project itself uses pushstate, then the history will be flawed (one more history);
    2. The browser's Back button click and call History.back () will also be treated as a pressed back key.

But anyway, for the structure and logic of a simple jump page (just to return to the app), this method is pretty practical, right? hehe ~

Monitor the return key of an Android device from a Web page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.