You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by na...@apache.org on 2014/03/18 02:13:42 UTC

git commit: Fixing back button issue by utilizing onBackPressed instead of onKeyUp

Repository: cordova-amazon-fireos
Updated Branches:
  refs/heads/master 74ebe2bf6 -> 5e9cb8fbb


Fixing back button issue by utilizing onBackPressed instead of onKeyUp


Project: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/commit/5e9cb8fb
Tree: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/tree/5e9cb8fb
Diff: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/diff/5e9cb8fb

Branch: refs/heads/master
Commit: 5e9cb8fbb85f2295f1472f58df1117ab536ee888
Parents: 74ebe2b
Author: Archana Naik <na...@lab126.com>
Authored: Tue Mar 11 17:45:53 2014 -0700
Committer: Archana Naik <na...@lab126.com>
Committed: Mon Mar 17 18:13:29 2014 -0700

----------------------------------------------------------------------
 .../src/org/apache/cordova/CordovaActivity.java | 24 ++++--
 .../src/org/apache/cordova/CordovaWebView.java  | 86 ++++++++++----------
 2 files changed, 63 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/blob/5e9cb8fb/framework/src/org/apache/cordova/CordovaActivity.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java
index d69ba33..6d9dae3 100755
--- a/framework/src/org/apache/cordova/CordovaActivity.java
+++ b/framework/src/org/apache/cordova/CordovaActivity.java
@@ -1175,11 +1175,25 @@ public class CordovaActivity extends Activity implements CordovaInterface {
         this.runOnUiThread(runnable);
     }
 
+    /*
+     * Overriding the onBackPressed since it more accurately reflects when a back button is pressed within the context
+     * of this Activity. For instance if another Activity displayed on top of this one closes itself in onKeyDown, only
+     * the onKeyUp is received which would unintentionally navigate back.
+     */
+    @Override
+    public void onBackPressed() {
+        if (appView != null && (appView.isCustomViewShowing() || appView.getFocusedChild() != null)) {
+            appView.onBackPressed();
+        } else {
+            super.onBackPressed();
+        }
+    }
+
     @Override
     public boolean onKeyUp(int keyCode, KeyEvent event)
     {
-        if (appView != null && (appView.isCustomViewShowing() || appView.getFocusedChild() != null ) &&
-                (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU)) {
+        if (appView != null && (appView.isCustomViewShowing() || appView.getFocusedChild() != null) &&
+            keyCode == KeyEvent.KEYCODE_MENU) {
             return appView.onKeyUp(keyCode, event);
         } else {
             return super.onKeyUp(keyCode, event);
@@ -1196,9 +1210,9 @@ public class CordovaActivity extends Activity implements CordovaInterface {
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event)
     {
-        //Determine if the focus is on the current view or not
-        if (appView != null && appView.getFocusedChild() != null && (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU)) {
-                    return appView.onKeyDown(keyCode, event);
+        // Determine if the focus is on the current view or not
+        if (appView != null && appView.getFocusedChild() != null && keyCode == KeyEvent.KEYCODE_MENU) {
+            return appView.onKeyDown(keyCode, event);
         }
         else
             return super.onKeyDown(keyCode, event);

http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/blob/5e9cb8fb/framework/src/org/apache/cordova/CordovaWebView.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java
index c6a28dd..aa80def 100755
--- a/framework/src/org/apache/cordova/CordovaWebView.java
+++ b/framework/src/org/apache/cordova/CordovaWebView.java
@@ -755,6 +755,49 @@ public class CordovaWebView extends AmazonWebView {
         return p.toString();
     }
 
+    /**
+     * Handle when the back button is pressed on the current window. Depending on the state of the application, this
+     * will either navigate back in the history, close the window, send a back event to the running web application,
+     * or dismiss a full screen video.
+     */
+    public void onBackPressed() {
+        // A custom view is currently displayed (e.g. playing a video)
+        if (mCustomView != null) {
+            this.hideCustomView();
+        } else {
+            // The webview is currently displayed
+            // If back key is bound, then send event to JavaScript
+            if (this.bound) {
+                this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
+                return;
+            } else {
+                // If not bound
+
+                // Give plugins a chance to override behavior
+                if (this.pluginManager != null) {
+                    Object returnVal = this.pluginManager.postMessage("onBackPressed", null);
+                    if (returnVal != null && returnVal instanceof Boolean && (Boolean) returnVal) {
+                        // The return value was a true boolean, callback was consumed
+                        return;
+                    }
+                }
+
+                // Go to previous page in webview if it is possible to go back
+                if (this.backHistory()) {
+                    return;
+                }
+                // If not, then invoke default behavior
+                else {
+                    // this.activityState = ACTIVITY_EXITING;
+                    // return false;
+                    // If they hit back button when app is initializing, app should exit instead of hang until
+                    // initialization (CB2-458)
+                    this.cordova.getActivity().finish();
+                }
+            }
+        }
+    }
+
     /*
      * onKeyDown
      */
@@ -780,10 +823,6 @@ public class CordovaWebView extends AmazonWebView {
                 return super.onKeyDown(keyCode, event);
             }
         }
-        else if(keyCode == KeyEvent.KEYCODE_BACK)
-        {
-            return !(this.startOfHistory()) || this.bound;
-        }
         else if(keyCode == KeyEvent.KEYCODE_MENU)
         {
             //How did we get here?  Is there a childView?
@@ -807,45 +846,8 @@ public class CordovaWebView extends AmazonWebView {
     @Override
     public boolean onKeyUp(int keyCode, KeyEvent event)
     {
-        // If back key
-        if (keyCode == KeyEvent.KEYCODE_BACK) {
-            // A custom view is currently displayed  (e.g. playing a video)
-            if(mCustomView != null) {
-                this.hideCustomView();
-            } else {
-                // The webview is currently displayed
-                // If back key is bound, then send event to JavaScript
-                if (this.bound) {
-                    this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
-                    return true;
-                } else {
-                    // If not bound
-
-                    // Give plugins a chance to override behavior
-                    if (this.pluginManager != null) {
-                        Object returnVal = this.pluginManager.postMessage("onBackPressed", null);
-                        if (returnVal != null && returnVal instanceof Boolean && (Boolean) returnVal) {
-                            // The return value was a true boolean, callback was consumed
-                            return true;
-                        }
-                    }
-
-                    // Go to previous page in webview if it is possible to go back
-                    if (this.backHistory()) {
-                        return true;
-                    }
-                    // If not, then invoke default behavior
-                    else {
-                        //this.activityState = ACTIVITY_EXITING;
-                    	//return false;
-                    	// If they hit back button when app is initializing, app should exit instead of hang until initialization (CB2-458)
-                    	this.cordova.getActivity().finish();
-                    }
-                }
-            }
-        }
         // Legacy
-        else if (keyCode == KeyEvent.KEYCODE_MENU) {
+        if (keyCode == KeyEvent.KEYCODE_MENU) {
             if (this.lastMenuEventTime < event.getEventTime()) {
                 this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
             }