Prevent closing InAppBrowser - Cordova - android

I have an simple app that onDeviceReady starts the InAppBrowser and shows a website. Then The InappBrowser will be closed when a specific event occurs.
As you may know, on Android platform pressing "back button" closes the InAppBrowser which I want to be prevented. I want to InAppBrowser be shown to user until that event occurs and user be unable to close the InAppBrowser.
Be noted that I am not talking about hardwareback options. hardwareback is a useful option which lets user goes back in history of his/her navigation by pressing "back button" but at the first page (when there is nothing left in history), it closes InAppBrowser while I want InAppBrowser still remains open.

In the latest InAppBrowser versions you still have to change the onBackPressed function in the JAVA code of InAppBrowserDialog.java plugin code:
public void onBackPressed () {
if (this.inAppBrowser == null) {
this.dismiss();
} else {
// better to go through the in inAppBrowser
// because it does a clean up
if (this.inAppBrowser.hardwareBack() && this.inAppBrowser.canGoBack()) {
this.inAppBrowser.goBack();
} else {
// this.inAppBrowser.closeDialog();
}
}
}
I don't like this approach I have asked a feature on GitHub; you can follow it on GitHub here: https://github.com/apache/cordova-plugin-inappbrowser/issues/530

I realize this question is fairly old but I stumbled upon it when I was searching for the answer myself.
You can edit the InAppBrowserDialog.java file (plugins>cordova-plugin-inappbrowser>src>android) to change the behavior of the back button once it's pressed.
this.inAppBrowser.closeDialog()
Comment out the above piece of code in the onBackPressed() method, and this will prevent the browser from being closed on hardware back, while still maintaining the navigation functionality.

Actually, there is no solution available to prevent the inappbrowser from closing when the hardware back button is pressed. The only thing you can do is reload the same URL in the inappbrowser again after a minimum delay of 500ms. The delay is important, otherwise, the inappbrowser window won't be shown.

Related

Override done button functionality inappbrowser

Is it possible to override done button functionality? i need to just hide inappbrowser on done button as when show same the last visit url next time.
First, you can hide the Done button in Android (setting option Footer = false) but not in iOS. This makes sense because in Android you have the Back button to close the browser and come back to the app. But iOS does not have back button, so the user needs a graphical button for it.
Second, you can't override the button functionality, but you can add events to the browser, to run your code when something happens, for example when the browser is closed, all of this is explained in the Github page:
https://github.com/apache/cordova-plugin-inappbrowser
browser. addEventListener( "exit", () => {
// Here do something
}) ;
Add closebuttoncaption=Done
Example:
cordova.InAppBrowser.open(url, '_blank', 'location=no,toolbar=yes,toolbarposition=top,closebuttoncaption=Done);

Android hardware back button action never fires (Ionic)

I tried adding function to override current back button behaviour, which is exactly the same as pressing back button (left arrow) in browser. This often goes to the view that is forward instead of going back. I understand that this is how WebView would handle back button action if it is not override.
I was surprised that Android back button doesn't work the same as ion-nav-view standard back button, so I found the code that is responsible in ionic.bundle.js:
// Triggered when devices with a hardware back button (Android) is clicked by the user
// This is a Cordova/Phonegap platform specifc method
function onHardwareBackButton(e) {
console.log('check'); //this never fires
var backView = $ionicHistory.backView();
if (backView) {
// there is a back view, go to it
backView.go();
} else {
// there is no back view, so close the app instead
ionic.Platform.exitApp();
}
e.preventDefault();
return false;
}
console.log('check2'); //this fires
$ionicPlatform.registerBackButtonAction(
onHardwareBackButton,
IONIC_BACK_PRIORITY.view
);
I tested it on Galaxy S5.
Any hints how to make this work?
Why are you not using : $ionicHistory.goBack() instead of backView.go(); ?
From doc goBack is the way to navigate through history
I think you are adding the the back view as a new view upon the stack by calling
backView.go();
you should, instead call
$ionicHistory.goBack();
Is it possible you're using Ionic View to run the app? If so, that's been one of the many different-than-native issues that I've run into while using Ionic View. I couldn't get the back button behavior to work as intended within Ionic View, among other things.
I've since switched to testing my app with others using Google Play Alpha/Beta Testing. That will get the app running on every tester's device the way it will actually appear.
Even though I had no errors like:
cordova is undefined
I added the following to my index.html and it started working
<script src="cordova.js"></script>

Back button with Cordova/phonegap

I am creating an application using the framework cordova. I need to intercept the "Back" button of the phone itself in order to perform the various aoperazioni, for example closing a modal dialog. The problem is that now, once you open the modal, I can not close it by pressing the back button. What happens is that you close any application. How could I do to intercept this event which is not part of the app but the phone? I refer to the actual button of a smartphone. Thanks for your help.
You can achieve this with Javascript using Phonegap's Event API
document.addEventListener("backbutton", function (e) {
// your code
} , false);
Have you read the docs?
Here's what you need: http://docs.phonegap.com/en/4.0.0/cordova_events_events.md.html#backbutton

Close the application on back button of android phones

I'm developing a app using angularjs and ionic framework. It has a logout option also. When i logout from my app it goes straight to the home page.But if i press the back button it'll go to page that can be accessed only if you are logged in. How can i create a option to close the app if you press the back button from the home page? I use angularjs.
I used this way but did not work
document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('/templates/playlists')){
/*
Event preventDefault/stopPropagation not required as adding backbutton
listener itself override the default behaviour. Refer below PhoneGap link.
*/
//e.preventDefault();
navigator.app.exitApp();
}
else {
navigator.app.backHistory()
}
}, false);
Your code is good.
But, I'm not sure about the second line : are you sure that the active page id is /templates/playlists ?
Normally, the value you should pass to the $.mobile.activePage.is function should contain a page id (which could be retrieved like this : $.mobile.activePage.attr('id')).
(by the way, I don't know which of jQuery Mobile you are using, but the $.mobile.activePage.is was deprecated).

Phonegap android app exiting after clicking on back button even though the user is logged-in

I am creating a simple android Login app using PhoneGap and Eclipse and facing problem with android back button.
I want to control the behaviour of back button such as :
If user has been logged in and he/she pressed on back button app should be minimized.
If not logged-in app should get closed after clicking on back button.
I scoured the lots of posts regarding with "Minimizing the app after clicking back button"
but didn't got the solution. Any solution?
You can handel phonegap back button events with the help of
document.addEventListener("backbutton", onBackButtonFire, false);
Handle Application Pause
document.addEventListener("pause", onPause, false);
Handle Application Resume
document.addEventListener("resume", onResume, false);
Define Back Function
function onBackButtonFire()
{
// Check if user is logged in or not.?
if(localStorage.Checklogin=="true")
{
// minimize app here
}
else
{
// Exit the app
navigator.app.exitApp();
}
}
function onPause()
{
//handel pause;
}
function onResume()
{
// handel resume of app
}
store your information about is user logged in in localStorage.Checklogin .
you can find manually pause the application using this link
Manually Make application Pause
The default behaviour of the backbutton is navigate back in the history and to close the app if ther's no more history.
In Cordova/Phonegap, you can build your own event handler for the back button.
See doc here : http://cordova.apache.org/docs/en/3.5.0/cordova_events_events.md.html#backbutton
Then in your event handler, you would use the history api to either navigate back if you're not on the first page or perform your own logic to close/reduce the app depending on user logged or not.
It's easy to close the app : navigator.app.exitApp();
But to reduce it, I don't know if there's an easy thing in phonegap. And I honestly don't think it makes any sence as in android there's the home button which basically reduces the app.
What I do in my app is that if user is logged, I display a dialog to ask the user if he wants to disconnect.

Categories

Resources