Back Navigation in the Android Webview App

I am working on a web browser application. I can not fix the problem in my application to go back. I use this code and tried to make all the changes.

public class DeviceActivity extends Activity { private WebView web; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); web = (WebView) findViewById(R.id.webView); web.getSettings().setJavaScriptEnabled(true); web.getSettings().setJavaScriptCanOpenWindowsAutomatically(false); web.getSettings().setPluginsEnabled(false); web.getSettings().setSupportMultipleWindows(false); web.getSettings().setSupportZoom(false); web.setVerticalScrollBarEnabled(false); web.setHorizontalScrollBarEnabled(false); //Our application main page will be loaded web.loadUrl("file:///android_asset/fbp/index.html"); web.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } public void onBackPressed (){ if (web.canGoBack()) { web.goBack(); } else { //My exit alert code goes here. } } }); } } 

This is not a backward move, but instead it exits the application. Any help would be appreciated.

+6
source share
3 answers

Add this to your Activity code (not in onCreate() or nested elsewhere)

 @Override public void onBackPressed() { if (web.copyBackForwardList().getCurrentIndex() > 0) { web.goBack(); } else { // Your exit alert code, or alternatively line below to finish super.onBackPressed(); // finishes activity } } 

This will cause the WebView history to be WebView back to the stack until it becomes empty, and then performs the default action (in which case it will complete the action).

You should probably also establish that your WebViewClient has shouldOverrideUrlLoading return true or you will not load any links.

+16
source

In your code, you saved the next block in the onCreate () method.

 if (web.canGoBack()) { web.goBack(); } else { //My exit alert code goes here. } 

Instead, save the same code in onBackPressed (), like this

 @Override public void onBackPressed() { if(web.canGoBack()) { web.goBack(); } else { super.onBackPressed(); } } 

also refer to Getting Started: WebView-based applications for Android web developers

0
source

We can use the webview method to navigate forward and backward. The logical canGoForward() method for Gets whether this WebView has a forwarding history element. canGoBack() for Gets whether this WebView has a history history element. goBack() returns to the history of this WebView and goForward() returns to the history of this WebView. this is my implementation:

 @Override public void onClick(View v) { switch (v.getId()){ case R.id.forward_btn: if (mWebView.canGoForward()){ mWebView.goForward(); }else{ Toast.makeText(getApplicationContext(), "Tidak ada halaman lagi!", Toast.LENGTH_SHORT).show(); } break; case R.id.backward_btn: if (mWebView.canGoBack()){ mWebView.goBack(); }else{ Toast.makeText(getApplicationContext(), "Tidak bisa kembali!", Toast.LENGTH_SHORT).show(); } break; case R.id.reload_btn: layoutLoading.setVisibility(View.VISIBLE); mWebView.reload(); break; case R.id.back_btn: onBackPressed(); break; } } 
0
source

Source: https://habr.com/ru/post/946595/


All Articles