Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-6391

Should be possible to intercept exiting of web app

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.4.0
    • Fix Version/s: None
    • Component/s: cordova-android
    • Labels:
    • Environment:

      Android

      Description

      I am integrating Cordova into an existing Android app, using CordovaWebView.

      When the web app exits (due to pressing back button beyond history), the CordovaWebView invokes finish() on the Activity. This is not always what you want. If you use Fragments, you might want to swap fragments rather than exit the Activity.

      Here is a patch to fix this (based on 3.4.0):

      diff --git a/org/apache/cordova/CordovaActivity.java b/org/apache/cordova/CordovaActivity.java
      index 9f8b020..a6d3502 100755
      --- a/org/apache/cordova/CordovaActivity.java
      +++ b/org/apache/cordova/CordovaActivity.java
      @@ -829,6 +829,7 @@ public class CordovaActivity extends Activity implements CordovaInterface {
               }
           }
       
      +    @Override
           /**
            * End this activity by calling finish for activity
            */
      diff --git a/org/apache/cordova/CordovaInterface.java b/org/apache/cordova/CordovaInterface.java
      index 59ed486..262d7ca 100755
      --- a/org/apache/cordova/CordovaInterface.java
      +++ b/org/apache/cordova/CordovaInterface.java
      @@ -69,4 +69,11 @@ public interface CordovaInterface {
            * Returns a shared thread pool that can be used for background tasks.
            */
           public ExecutorService getThreadPool();
      +
      +    /**
      +     * Called when the web app exits.
      +     * Normally, you would just call {@link android.app.Activity#finish()},
      +     * but a sophisticated application might want to do something else like swapping fragments.
      +     */
      +    public void endActivity();
       }
      diff --git a/org/apache/cordova/CordovaWebView.java b/org/apache/cordova/CordovaWebView.java
      index 4dfeb55..28e9561 100755
      --- a/org/apache/cordova/CordovaWebView.java
      +++ b/org/apache/cordova/CordovaWebView.java
      @@ -760,7 +760,7 @@ public class CordovaWebView extends WebView {
                               //this.activityState = ACTIVITY_EXITING;
                              //return false;
                              // If they hit back button when app is initializing, app should exit instead of hang until initi
      -                       this.cordova.getActivity().finish();
      +                       this.cordova.endActivity();
                           }
                       }
                   }
      

        Attachments

          Activity

            People

            • Assignee:
              agrieve Andrew Grieve
              Reporter:
              mikaelstaldal Mikael Ståldal
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: