webview with iframe - back button goes back inside the iframe - android

I have an android project, that shows only a web view.
I use this function to go to the previous page with the back button of the device:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
webView = findViewById(R.id.webView);
if (webView.canGoBack()) {
webView.goBack();
return true;
} else {
// alert dialog...
}
}
return super.onKeyDown(keyCode, event);
}
and this function to go back to the previous page via js inside the web view:
goBack()
{
history.go(-1);
}
In these both functions, if the page contains an iframe, the page inside the iframe goes back, and not the parent page.
How can I make the parent page go back and not the page inside the iframe go back on clicking the back button?
Thanks!

try like this
goBack() {
if(webView.canGoBack()) {
webView.goBack();
} else {
finish()
}
}

Related

How to use back button to go back with XWalkView of CrossWalk, or disable it?

I use below code to go back in webview at first try. But for the low render ability, I used XWalkView replace the WebView.
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView mWebView = (WebView) findViewById(R.id.webview);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
if (MainActivity.mTencent.isSessionValid()) {
MainActivity.logout();
}
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
When change to XWalkView, I only find this about go back in the XWalkView. But I cannot find an example about to use it.
When I not implement the back button envent, the app will exit after I double click the back button.
My question is:
1. How to use go back in the XWalkView, if some code may be more helpful.
2. How can I disable the back button click event, when I not use the go back functon.
Thank you in advance.
After days of digging, I solved this: put this in the activity xwalkview in. Though this works, but the go back sometimes lost some history. So I also want someone give a better answer here.
for goback:
public boolean onKeyDown(int keyCode, KeyEvent event) {
//WebView mWebView = (WebView) findViewById(R.id.webview);
XWalkView mXWalkView = (XWalkView) findViewById(R.id.xWalkView);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mXWalkView.getNavigationHistory().canGoBack()) {
mXWalkView.getNavigationHistory().navigate(XWalkNavigationHistory.Direction.BACKWARD, 1) ;
} else {
finish();
if (MainActivity.mTencent.isSessionValid()) {
MainActivity.logout();
}
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
for disable back event you can override either of these method dispatchKeyEvent, onBackPressed, onKeyDown. Refer to this answer for more.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.dispatchKeyEvent(event);
}
You can use xwalkviewhistory.cangoback()

webview activity in android

I have two activity let A and B. A is listActivity and B is webViewActivity. Now in webview, when i back pressed then it works fine within webview but in last back pressed, it resides in activity B but I want to go back to Activity A (listActivity).
My code for back pressed
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thanks.
This may help you...
http://rickluna.com/wp/2014/04/disable-android-back-button-in-inappbrowser/
here it explains that you can disable back button in webview... so make this works in your logic.
#Override
public void onBackPressed() {
super.onBackPressed();
}
onbackpressed function call when you pressed back button then finish current activity.

ImageButton in ActionBar for GoBack in WebView

I'm using Fragments and one of my Fragment I have a webview. I want to add a image button that will let user to go back.
I have 4 Fragments and their names are Fragment1,Fragment2,Fragment3,Fragment4.
Also I have 4 layouts tab1.xml tab2.xml tab3.xml tab4.xml and there is 4 webview objects in all layouts.
When I click Fragment1, it opens tab1.xml and tab1.xml's webview1 opens www.site.com and user starts surfing inside webview1.
If user visits 2 or more pages in webview1 I want this user let can go back with a button.
How can I do this?
In your decalarations:
WebView web = null;
In your onCreateView;
// Prepare WebView.
web = (WebView) v.findViewById(R.id.htmlDisplay);
// Set WebView.
final WebSettings webSettings = web.getSettings();
// Enable JavaScript.
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
In your back button (not the physical one):
web.goBack()
If you want to use your physical back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(web.canGoBack() == true)
{
web.goBack();
}
else
{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Using the Back button in a webview in Android

When using the normal mobile browser, when you press the Back button, it takes you to the previous page. When a WebView is used in my app to display a web page and the user wants to return to the previous page, how is typically done? Does pressing the Back button automatically take the user back to the previous page or does it take them back to the previous Activity? I would assume that since the Back button is normally meant to take the user to the previous activity, it shouldn't be used to return to a previous web page. If this is the case, should my mobile web page include its own Back link that a user clicks on?
I guess what I am trying to do is to understand the correct behavior I should be employing.
Let's look at some popular browser. currently, in my device, there are two browsers
chrome
default internet browser
In both browsers, it goes to the previous page. So it is a common behavior. to do so
you can overwrite the onBackPressed to go to your previous page.
#Override
public void onBackPressed(){
if(mWebView.canGoBack() == true){
mWebView.goBack();
}
else{
finish();
}
}
Please keep in mind that back buttons is coming with majority of the android devices. So you do not need a back button in your activity to go back to previous activity.
And about the webview if you want navigation in the webview to travel all the previous pages then you can put a back button and implement the navigation of the webview using the method webview.goBack().
In short, If you want your user to navigate to previous page then you should give functionality of the webview to go back to previous page and from the first page user click on back button again finish the activity.
You can use this override the onkeydown event and check the key pressed (in this case is back key) and check if is there any previous page
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
When you are clicking back button, you are clicking it for activity that is opening the webview better thing would be to give a back button in you layout..and hence saving the last url opened by user
Something like this in activity with WebView:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
or override onBackPressed method:
#Override
public void onBackPressed() {
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
} else {
finish();
}
}

Android webview back button

In my app when user clicks on a gallery widget i open the image in a webview as it supports zoom functionality.
Now when user clicks back button on webview i want the user to see my application. But it goes directly to home screen.
How should i handle the onBackKeyPressed() ?
Override the Activity's onKeyDown(..) event
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// close image gallery
return false; // this avoids passing to super
}
return super.onKeyDown(keyCode, event);
}
*edit: code
#Override
public onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();

Categories

Resources