I have a sapui5 mobile application which runs perfectly. The android device has a back button. What I want to achieve is that when you press the button the app should navigate back.
In the sapui5 mobile api it is possible with this coding:
app.back();
The question now is how I can make this?
The activity of my android application has the following method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//Here you should navigate one view back
}
return super.onKeyDown(keyCode, event);
}
Is it possible to call from my activity the app variable in javascript and then tell my application to navigate back?
Greetings
Stef
I found a solution for solving the problem ... I wrote a new javascript file with the following code:
function onBackKeyDown(){
// Handle here the BackButton
}
function onDeviceReady(){
document.addEventListener("backbutton", onBackKeyDown, false);
}
function init(){
document.addEventListener("deviceready", onDeviceReady, false);
}
I took this out of the phonegap api and works perfectly!
Greetings
Stef
Related
I'm using the following code to capture the back button press in a Cordova Android 4.0.0 App with Crosswalk:
// Wait for device API libraries to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
//
function onBackKeyDown(e) {
e.preventDefault();
alert("Back button pressed!");
}
The button handler is called but using e.preventDefault() is not working and the App is still being closed.
Does anyone know why or a workaround for this?
Please help me on this.....
I am using phonegap-1.1.0v . In my application i have multiple HTML file. Each file is a single page
Each page contains the home button on top of every HTML page.
when i moved from Homepage -- > screen B --> Again Homepage --> screen c
when i pressed Back key on Screen c it goes back to Homepage but when i press the Backkey again it goes to Screen B and the Homepage.
For MY solution i requires when i press the Back key from home screen it always need to Flash screen page (or) it should get exit from the application.
Thanks in Advance.............:)
Here is how you do
document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('#homepage')){
e.preventDefault();
navigator.app.exitApp();
}
else {
navigator.app.backHistory()
}
}, false);
//Deviceready function
document.addEventListener('deviceready', function() {
document.addEventListener("backbutton", go_back, false);
}, false);
function go_back(){
// Put your code when back key is press
// U can use window.location="home.html" to change page or something else
// If u want to exit then use "device.exitApp();"
}
For that you have to take a global Boolean variable and when you reach at home make it true and check this variable every time when you press back button like :
if (device.platform == 'android')
{
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e)
{
if(isHome)
{
navigator.app.exitApp();
}
else
{
navigator.app.backHistory();
//What you want when its not home page
}
}
}
I'm creating a jQuery Mobile app which is contained within a Titanium app's webview. When the Android back button is pressed, it exits the app rather than going to the previous page (not the best default behavior in my opinion.) I'm trying to get around this by executing the following code on app load:
document.addEventListener("deviceready", function () {
alert("adding back button event");
document.addEventListener("backbutton", function (e) {
alert("in back button event");
if ($.mobile.activePage.is('#homepage')) {
alert("exit app");
e.preventDefault();
navigator.app.exitApp();
}
else {
alert("go back");
navigator.app.backHistory()
}
}, false);
}, false);
Everywhere I see threads about this issue, they say to use the backbutton event, however it's not being fired at all. If I wrap it in a deviceready event, like above, then deviceready isn't called either. Is the webview somehow suppressing these events, or is there another way to do this with jQuery Mobile?
Your example will not work because you are trying to use Phonegap API call for backbutton handling.
You need to use something appropriate to Appcelerator. First you should check out the canGoBack, canGoForward, goBack and goForward functions for webViews.More info can be found here.
Basically you will need to create function that will handle a backbutton in your webview. Something like this:
detailContainerWindow.addEventListener('android:back', function(e){
if (detailView.canGoBack()) {
detailView.goBack();
} else {
detailContainerWindow.close();
}
});
I'm a bit new to Android programming so bear with me if I'm not 100% correct on how i phrase my questions. I'm looking for an a way to stop and reload my Android webView. The webView takes the user to a a simple HTML link, which works. Now I want to implement stop, reload, forward, and back buttons for the webView. What I did for the back and forward functions was very simple-- I simply created an onKeyDown function to facilitate going forward and backward as shown below:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
if((keyCode == KeyEvent.KEYCODE_FORWARD) && mWebView.canGoForward())
{
mWebView.goForward();
return true;
}
return super.onKeyDown(keyCode, event);
}
Now for reloading (refreshing) this webView and stopping this webView from loading is my trouble. I know for reloading a webView you could simply create a new setOnClickListener function and reload the URL. I'm not so sure about stopping a webView from loading. Do I have to create individual buttons for reloading/stopping the webView? Or is there a prebuilt function I can place inside my code that can help me do so? Any help would be appreciated! Thanks!
Reload
mWebview.loadUrl("http://twitter.com"); //calling again `loadUrl()` with `url` gives your refresh type effects.
I think there ain't any method like reloadUrl.
Stop Loading
stopLoading (): Stops the current load.
mWebView.stopLoading();
Reloading the WebView
mWebView.reload();
Stop loading URL
mWebView.stopLoading ()
it will 100% work
I load HTML data into WebView with loadDataWithBaseURL
Do it one more time
Execute the following code and instead of going back to the 1st
page - whole app exits. What am I doing wrong here?
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && this.browser.canGoBack()) {
this.browser.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
Also - is it possible for WebView cache to survive Activity#onStop?
Basically - if I close app and reopen - I want WebView to display last
data that was loaded, currently - I'll get a blank screen and then
have to reload same data again
The problem is that load* does not create a new WebView, nor does it do anything special like create a history record, unfortunately.
You probably want call startActivity() and invoke a second activity for the second set of data.