Improve closing an HTML page and returning to previous page.

This commit is contained in:
Bryce Curtis 2011-09-29 11:02:54 -05:00
parent a4d66c63a4
commit ebb9f09168
3 changed files with 33 additions and 10 deletions

View File

@ -59,6 +59,14 @@ App.prototype.clearHistory = function() {
PhoneGap.exec(null, null, "App", "clearHistory", []); PhoneGap.exec(null, null, "App", "clearHistory", []);
}; };
/**
* Go to previous page displayed.
* This is the same as pressing the backbutton on Android device.
*/
App.prototype.backHistory = function() {
PhoneGap.exec(null, null, "App", "backHistory", []);
};
/** /**
* Override the default behavior of the Android back button. * Override the default behavior of the Android back button.
* If overridden, when the back button is pressed, the "backKeyDown" JavaScript event will be fired. * If overridden, when the back button is pressed, the "backKeyDown" JavaScript event will be fired.
@ -91,6 +99,8 @@ App.prototype.addWhiteListEntry = function(origin, subdomains) {
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {
navigator.app = new App(); navigator.app = new App();
navigator.app.origHistoryBack = window.history.back;
window.history.back = navigator.app.backHistory;
}); });
}()); }());
} }

View File

@ -44,6 +44,9 @@ public class App extends Plugin {
} }
else if (action.equals("clearHistory")) { else if (action.equals("clearHistory")) {
this.clearHistory(); this.clearHistory();
}
else if (action.equals("backHistory")) {
this.backHistory();
} }
else if (action.equals("overrideBackbutton")) { else if (action.equals("overrideBackbutton")) {
this.overrideBackbutton(args.getBoolean(0)); this.overrideBackbutton(args.getBoolean(0));
@ -144,9 +147,19 @@ public class App extends Plugin {
/** /**
* Clear web history in this web view. * Clear web history in this web view.
* This does not have any effect since each page has its own activity.
*/ */
public void clearHistory() { public void clearHistory() {
((DroidGap)this.ctx).clearHistory(); ((DroidGap)this.ctx).clearHistory();
// TODO: Kill previous activities?
}
/**
* Go to previous page displayed.
* This is the same as pressing the backbutton on Android device.
*/
public void backHistory() {
((DroidGap)this.ctx).endActivity();
} }
/** /**
@ -174,7 +187,7 @@ public class App extends Plugin {
*/ */
public void exitApp() { public void exitApp() {
((DroidGap)this.ctx).setResult(Activity.RESULT_OK); ((DroidGap)this.ctx).setResult(Activity.RESULT_OK);
((DroidGap)this.ctx).onDestroy(); ((DroidGap)this.ctx).endActivity();
} }
/** /**

View File

@ -716,13 +716,6 @@ public class DroidGap extends PhonegapActivity {
if (this.appView != null) { if (this.appView != null) {
// Make invisible
DroidGap.this.runOnUiThread(new Runnable() {
public void run() {
DroidGap.this.setVisible(false);
}
});
// Make sure pause event is sent if onPause hasn't been called before onDestroy // Make sure pause event is sent if onPause hasn't been called before onDestroy
this.appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};"); this.appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};");
@ -816,7 +809,7 @@ public class DroidGap extends PhonegapActivity {
// Finish current activity // Finish current activity
if (clearPrev) { if (clearPrev) {
this.finish(); this.endActivity();
} }
} }
@ -1300,6 +1293,13 @@ public class DroidGap extends PhonegapActivity {
} }
} }
/**
* End this activity by simulating backbutton keypress
*/
public void endActivity() {
super.onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
}
/** /**
* Called when a key is pressed. * Called when a key is pressed.
* *
@ -1408,7 +1408,7 @@ public class DroidGap extends PhonegapActivity {
// If terminating app, then shut down this activity too // If terminating app, then shut down this activity too
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
this.setResult(Activity.RESULT_OK); this.setResult(Activity.RESULT_OK);
this.onDestroy(); this.endActivity();
} }
return; return;
} }