Implementing Navigation Back Feature to Android WebView Application
来源:互联网 发布:js queryselectorall 编辑:程序博客网 时间:2024/04/29 00:36
http://www.viralandroid.com/2015/08/implementing-navigation-back-feature-to-android-webview.html
By default, Navigation Back Feature is disabled in android WebView. You can enable it by OverridingonKeyDown method, so in this tutorial I will teach you how to Implement Navigation Back Feature to Android WebView. To get back from your android WebView application, you need to press back button from android device.
Implementing Navigation Back Feature to Android WebView Demo:
Use Following Code to Implement Navigation Back Feature:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack(); // Go to previous page
return true;
}
// Use this as else part
return super.onKeyDown(keyCode, event);
}
Simple Android WebView Example and Tutorial
How to Load HTML Data/String on Android WebView Application
Step 1: Create Android Project:
Create a new android project for Implementing Navigation Back Feature to Android WebView Application with following information:
Application name: Adding Navigation Back Feature to Android WebView
Company Domain: sirseni.com
Package name: com.sirseni.addingnavigationbackfeaturetoandroidwebview
Minimum SDK: Android 2.2 (API 8 Froyo)
Step 2: Adding WebView to Your activity_main.xml file:
res/layout/activity_main.xml
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myWebView"
android:scrollbars="none"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Step 3: Modify your MainActivity.java file:
src/MainActivity.java
/* Implementing Navigation Back Feature to Android WebView */
package com.sirseni.addingnavigationbackfeaturetoandroidwebview;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("http://www.centerend.com");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.centerend.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack(); // Go to previous page
return true;
}
// Use this as else part
return super.onKeyDown(keyCode, event);
}
}
Add the INTERNET Permission to Your AndroidManifest File
You must add the INTERNET permission to your AndroidManifest.xml file to load website in your application. So before running your WebView application, add single line code to your Android Manifest file.
<uses-permission android:name="android.permission.INTERNET" />
Step 4: Modify AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sirseni.addingnavigationbackfeaturetoandroidwebview">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now run your Implementing Navigation Back Feature to Android WebView application by just clicking Run icon. To see how Navigation Back Feature works, just click one of home screen icon like HTML, CSS, Bootstrap and for back to home page you need to press back button from your android device.
Simple Android WebView Example and Tutorial
How to Load HTML Data/String on Android WebView Application
Simple Android WebView Example and Tutorial
How to Load HTML Data/String on Android WebView Application
Implementing Navigation Back Feature to Android WebView Demo:
Download complete Implementing Navigation Back Feature to Android WebView Application example project source code from GitHub.
Download Complete Example Project
Download complete Implementing Navigation Back Feature to Android WebView Application example project source code from GitHub.
0 0
- Implementing Navigation Back Feature to Android WebView Application
- android-Implementing Descendant Navigation,Notifying the User
- android-Providing Up Navigation,Providing Proper Back Navigation
- android-Implementing Effective Navigation,Creating Swipe Views with Tabs
- Back to Android
- 2.12 Implementing Navigation with UINavigationController
- android back to privious activity
- Android Webview HTML5 Application cache
- iFrame 项目总结 how to use Navigation-based application
- Implementing Cross-Browser Compatible Smart Navigation Features
- Implementing Lateral Navigation 实现横向导航
- Implementing Ancestral Navigation 实现原始导航
- Implementing Temporal Navigation 实现时间导航
- Implementing Descendant Navigation 实现下一代导航
- 使用Navigation timing api测试android webview性能
- Navigation with Back and Up
- How to add SMSC feature into Android
- Implementing FSD Call-back File Notifications
- 摄像头之自动曝光,自动增益,图像深度 概念
- Linux系统下重要的分区及其作用
- 虚拟现实的起源、发展、爆发与沉淀
- Java对象在JVM中的生命周期
- 爱上PowerShell
- Implementing Navigation Back Feature to Android WebView Application
- Activity 相关属性总结
- 线性表单链式存储结构的操作
- Swift一等函数:函数作为参数传递或返回
- 个人电脑常用工具汇总
- Selenium2Java自动化测试实战读书笔记(一)
- Spring Boot原理解析1
- 一些常用的前端基础操作
- 在GitHub上收集的最全的前端资源汇总(包括前端学习、前端开发资源、前端求职面试等)