ภาพรวม
ตัวอย่างนี้สาธิตวิธีการทำให้ WebView สามารถนำทางไปหน้าภายในเว็บไซต์ (Internal Navigation) ได้อย่างถูกต้อง โดยใช้ WebViewClient.shouldOverrideUrlLoading() และจัดการปุ่ม Back ด้วย onBackPressed()
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project กรอกข้อมูลที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ในโค้ดข้างต้น เราใช้ WebView เต็มหน้าจอเพื่อโหลด Facebook (m.facebook.com/touch.facebook.com)
ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)
package com.example.myapplication;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.setCancelable(false);
webView = findViewById(R.id.web_view);
webView.setVerticalScrollBarEnabled(true);
webView.requestFocus();
webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.getSettings().setJavaScriptEnabled(true);
// โหลด Facebook Touch version (เหมาะกับมือถือ)
webView.loadUrl("https://touch.facebook.com/");
// จัดการการนำทาง URL ภายใน WebView
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// โหลด URL ใน WebView เดียวกัน (ไม่เปิด Browser ภายนอก)
view.loadUrl(url);
return true; // บอกว่าเราจัดการเอง
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress < 100) {
progressDialog.show();
} else {
progressDialog.dismiss();
}
}
});
}
// จัดการปุ่ม Back - กลับหน้าก่อนหน้าใน WebView ก่อน
@Override
public void onBackPressed() {
if (webView != null && webView.canGoBack()) {
webView.goBack(); // กลับหน้าก่อนหน้าใน WebView
} else {
super.onBackPressed(); // ไม่มีหน้าก่อนหน้า -> ปิดแอป
}
}
}
คำอธิบายสำคัญ:
WebViewClient.shouldOverrideUrlLoading()- สำคัญที่สุด จับการคลิกลิงก์และโหลดใน WebView เดียวกัน (return true = จัดการเอง)webView.canGoBack()/webView.goBack()- ตรวจสอบและนำทางกลับในประวัติ WebViewonBackPressed()- Override เพื่อให้ปุ่ม Back กลับหน้า WebView ก่อน แล้วค่อยปิดแอปtouch.facebook.com- เวอร์ชันมือถือของ Facebook เหมาะกับ WebView มากกว่าเวอร์ชัน Desktop
• Deprecated (API 24+) - เมธอดเก่า
shouldOverrideUrlLoading(WebView, String)• ใหม่ (API 24+) - ใช้
shouldOverrideUrlLoading(WebView, WebResourceRequest) แทน• โค้ดตัวอย่างใช้เมธอดเก่าเพื่อความเข้ากันได้ (ยังทำงานได้) แต่ควรย้ายไปใช้เมธอดใหม่ในอนาคต
โค้ดต้นฉบับมีเงื่อนไข
if (progress = = 100) ซึ่งเป็น Syntax Error (ต้องเป็น == ไม่มีช่องว่าง) - ได้แก้ไขแล้ว
ขั้นตอนที่ 4: กำหนดสิทธิ์ใน AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
การทดสอบแอปพลิเคชัน
เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ แล้วตรวจสอบหน้าจอ - จะแสดง Facebook Touch ใช้งานได้ปกติ นำทางหน้าภายในได้ กด Back กลับหน้าก่อนหน้าได้
