Hello!

I’ll say right away that I’m not a programmer, I’m a customer, so there’s something I’m not saying, I don’t throw stones, please.

I ordered a client to the site - a webview with access to only one site, in general they made it to me, but one problem arose - by default in webview, the system button back closes the application, and the link to the site is a href="javascript:history.go(-1) goes into the loop, that is - from page 1 the user goes to 2, and then to 3, and then presses the button back and from page 3 returns to 2, when the button is pressed back, the user again goes to page 3 , and not on page 1, and then from 3 again to 2, from 2 again to 3 and so forever. At the same time, if you write a link like a href="javascript:history.go(-10) href="javascript:history.go(-10) - the system will take 10 steps back, and then one forward and again a cycle.

The solution was found something like this:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } 

I honestly do not know what it is and where it is inserted, since I have already said that I am not a programmer, but I think you will understand.

In this case, the system back button works as expected - takes one step back, a link like a href="javascript:history.go(-X) " stops going into a loop, but a new problem appears, a link like a href="javascript:history.go(-X) "does not take X steps back, as before, but only 1, regardless of the value of X. In other webviews, I observed that the system button can be made to work fine, and the site is correct, but the programmer I ordered - does not know how to do it.

I looked for and found a possible solution - instead of goBack use goBackOrForward() , but the programmer to whom I ordered the application has no idea how to use it, and even more so.

In general, the problem in solving which I ask for help is to implement the work of the "back" button, both systemically and with the help of a script on a site like a href="javascript:history.go(-x) " in a webview on Android. It is necessary that the back button of the form history.go (-x) takes X steps back, while the system can either take a few steps back, or one step back, or even exit the application altogether - no matter. The main thing is that the history.go (-x) button works and does not get hung up.

Help please, knowledgeable people! Forgive me if I have clumsily explained, if anything - ask questions, I will try to answer! Thank!

update 05/25/2014

Here, unearthed the activation code:

 package mobi.infa.smolik; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.webkit.GeolocationPermissions.Callback; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends Activity { ProgressBar loadingProgressBar; ProgressBar loadingTitle; WebView mWebView; public static boolean checkInternetConnection(Context paramContext) { ConnectivityManager localConnectivityManager = (ConnectivityManager)paramContext.getSystemService("connectivity"); return (localConnectivityManager.getActiveNetworkInfo() != null) && (localConnectivityManager.getActiveNetworkInfo().isAvailable()) && (localConnectivityManager.getActiveNetworkInfo().isConnected()); } protected void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); requestWindowFeature(1); setContentView(2130903040); final Context localContext = getApplicationContext(); this.loadingProgressBar = ((ProgressBar)findViewById(2131165185)); this.mWebView = ((WebView)findViewById(2131165186)); this.mWebView.getSettings().setJavaScriptEnabled(true); this.mWebView.getSettings().setUserAgentString("infa_android_v0_1"); this.mWebView.loadUrl("http://www.site.ru"); this.mWebView.setWebChromeClient(new WebChromeClient() { public void onGeolocationPermissionsShowPrompt(String paramAnonymousString, GeolocationPermissions.Callback paramAnonymousCallback) { paramAnonymousCallback.invoke(paramAnonymousString, true, false); } public void onProgressChanged(WebView paramAnonymousWebView, int paramAnonymousInt) { super.onProgressChanged(paramAnonymousWebView, paramAnonymousInt); MainActivity.this.loadingProgressBar.setProgress(paramAnonymousInt); if (paramAnonymousInt == 100) { MainActivity.this.loadingProgressBar.setVisibility(4); return; } MainActivity.this.loadingProgressBar.setVisibility(0); } }); WebViewClient local2 = new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView paramAnonymousWebView, String paramAnonymousString) { if (paramAnonymousString.contains("infa.mobi")) { if (paramAnonymousString.equalsIgnoreCase(MainActivity.this.mWebView.getUrl())) { MainActivity.this.mWebView.goBack(); return true; } if (MainActivity.checkInternetConnection(localContext)) { MainActivity.this.mWebView.loadUrl(paramAnonymousString); return true; } Toast localToast = Toast.makeText(localContext, "Отсутствует интернет соединение!", 1); localToast.setGravity(17, 0, 0); localToast.show(); return true; } Intent localIntent = new Intent("android.intent.action.VIEW"); localIntent.setData(Uri.parse(paramAnonymousString)); MainActivity.this.startActivity(localIntent); return true; } }; this.mWebView.setWebViewClient(local2); } public boolean onCreateOptionsMenu(Menu paramMenu) { paramMenu.add("На главную"); paramMenu.add("Поделиться"); paramMenu.add("О программе"); paramMenu.add("Выход"); return super.onCreateOptionsMenu(paramMenu); } public boolean onBackPressed(int paramInt, KeyEvent paramKeyEvent) { if (paramKeyEvent.getAction() == 0) {} switch (paramInt) { default: return super.onBackPressed(paramInt, paramKeyEvent); } if (this.mWebView.canGoBack()) { this.mWebView.goBack(); } for (;;) { return true; finish(); } } public boolean onOptionsItemSelected(MenuItem paramMenuItem) { if (paramMenuItem.getTitle().toString().contains("главную")) { this.mWebView.loadUrl("http://www.site.ru/"); } if (paramMenuItem.getTitle().toString().contains("делиться")) { Intent localIntent = new Intent("android.intent.action.SEND"); localIntent.setType("text/plain"); localIntent.putExtra("android.intent.extra.TEXT", "http://www.site.ru"); localIntent.putExtra("android.intent.extra.SUBJECT", "Классный сайт!"); startActivity(Intent.createChooser(localIntent, "Поделиться")); } if (paramMenuItem.getTitle().toString().contains("ыход")) { finish(); } return super.onOptionsItemSelected(paramMenuItem); } } 
  • And why not just override the onBackPressed () method of the Activity, inside which the webView is located? - Lucky_spirit
  • I’ll ask the programmer, I’m just a web programmer myself, and unfortunately I don’t understand anything ((It looks silly of course, I just don’t want to lose because of this money I already paid for, so I resorted to this unconventional way - ask questions on a topic in which I myself do not understand anything. - moscow-zel
  • Updated post. The problem has not been solved - the programmer says that he does not know what to do. I picked up the application, found the activation code in the mainActivity.java file. Maybe the code can understand what the problem is? Thanks for the help! - moscow-zel
  • I can help, but you need to watch the project completely! - G-Force53

2 answers 2

I apologize, may not fully understand the question.

 public class MainActivity extends AppCompatActivity { private WebView view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.view = (WebView) findViewById(R.id.webview); view.loadUrl("http://www.plus2net.com/javascript_tutorial/history-object.php"); view.getSettings().setJavaScriptEnabled(true); } @Override public void onBackPressed() { if (view.canGoBack()) { view.goBack(); } else { super.onBackPressed(); } } } 

Perfectly fulfills your requirements. By clicking on the back goes all the way to the Histori Webview and then closes the activation. If you are in the downloaded page, click on the demo of the history object , then go to the page where history.go (-1) is used. By clicking on go -1 go back.

     view.getSettings().setJavaScriptEnabled(true); 

    Try true change to false

    • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky