ImageButton in ActionBar for GoBack in WebView - android

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);
}

Related

webview with iframe - back button goes back inside the iframe

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()
}
}

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()

Need to display a website on a fragment

I am trying to display an external website in a fragment on a webview. Although when i open the fragment, i get a blank screen. Kindly help me with the same.
public class one extends android.support.v4.app.Fragment {
public one() {
// Required empty public constructor
}
private WebView mWebview ;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FrameLayout framelayout = (FrameLayout) inflater.inflate(R.layout.fragment_one, container, false);
mWebview = (WebView) framelayout.findViewById(R.id.web);
WebSettings setting =mWebview.getSettings();
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
//webView.loadUrl("http://www.cleankutz.appointy.com");
mWebview.loadUrl("https://www.cleankutz.appointy.com");
return framelayout;
}
}
You need the following in the manifest:
<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
Here are some things you can try:
Make sure that internet is enabled in your device when testing
Enable java script:
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Also, I find this code quite useless:
mWebview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
That just overrides any link clicks. And if you want to do that, you should just not set the web view client. Also, just a bonus, if you want your webview to go back on back press, do this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
Please accept my answer by pressing the checkmark if this helped. If you have more questions, feel free to ask me.

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();
}
}

Current webview with CanGoBack feature inside Tab

I am creating application with dynamic tabhost, each tab has dynamic webview using the following tutorial,
http://www.pocketmagic.net/?p=1132
The above is created as Custom control(unable post the source code). I have extended this control with main activity. In keydown event i have checked the webview able to go back or not. If can, then it should load previous url in the current webview. But this is working if i have worked in the same tab. If i have moved two pages and shifted to second tab and then first tab, now pressing back button Application exists. Could you please tell me how to get the current webview to go back.
I have solved this Issue :-)
Solution:
1. Created One class named MyWebView that should extends WebView.
2. Created 4 MyWebView instances from User Side.
3. That MyWebView should have one WebView locally and it should not be static( here i did a mistake).
4. Override the Layout of WebView inside the MyWebView class with Progressbar and WebView inside the FrameLayout.
5. For every tab creation we have to set the Content as instance of MyWebView.
6. Now handle as follows,
UserClass.java
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView1!=null)
{
currentWebView= webView1.getWebView();
if(currentWebView!=null && currentWebView.canGoBack() == true)
{
currentWebView.goBack();
}
else
//Show Alert to Quit the Application
}
else
//Show Alert to Quit the Application
return true;
}
}
}
MyWebView.java:
public WebView getWebView()
{
return webview;
}
That is worked for me...

Categories

Resources