閲覧履歴をクリア

広告

ページ間を移動していくと履歴が内部的に残されていきます。ここでは閲覧履歴をクリアする方法を確認します。閲覧履歴をクリアするには「WebView」クラスで用意されている「clearHistory」メソッドを使います。

Tell the WebView to clear its internal back/forward list.

メソッドを実行すると履歴がクリアされます。

具体的には次のように記述します。

@Override protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    WebView webview = new WebView(this);

    /* ... */

    webview.clearHistory();
}

サンプルプログラム

それでは実際に試してみます。プロジェクトを作成しソースコードを次のように変更しました。

Test06_01.java

package jp.javadrive.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.view.View.OnClickListener;

public class Test06_01 extends Activity implements OnClickListener{

    private final int FP = ViewGroup.LayoutParams.FILL_PARENT; 
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 

    private Button buttonBack;
    private Button buttonForward;
    private Button buttonClearHistory;
    private WebView webview;

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);


        LinearLayout btnLinearLayout = new LinearLayout(this);
        btnLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

        buttonBack = new Button(this);
        buttonBack.setText("Back");
        buttonBack.setOnClickListener(this);

        buttonForward = new Button(this);
        buttonForward.setText("Forward");
        buttonForward.setOnClickListener(this);

        buttonClearHistory = new Button(this);
        buttonClearHistory.setText("Clear History");
        buttonClearHistory.setOnClickListener(this);

        btnLinearLayout.addView(buttonBack, createParam(WC, WC));
        btnLinearLayout.addView(buttonForward, createParam(WC, WC));
        btnLinearLayout.addView(buttonClearHistory, createParam(WC, WC));


        webview = new WebView(this);
        webview.loadUrl("http://www.google.co.jp/");

        linearLayout.addView(btnLinearLayout, createParam(FP, WC));
        linearLayout.addView(webview, createParam(WC, WC));
    }

    private LinearLayout.LayoutParams createParam(int w, int h){
        return new LinearLayout.LayoutParams(w, h);
    }

    public void onClick(View v) {
        if (v == buttonBack){
            if (webview.canGoBack()){
                webview.goBack();
            }
        }else if (v == buttonForward){
            if (webview.canGoForward()){
                webview.goForward();
            }
        }else if (v == buttonClearHistory){
            webview.clearHistory();
        }
    }
}

ビルド後にエミュレーター上で実行します。

閲覧履歴をクリア

初期値として指定したURLが表示されます。

では適当にいくつかのページを移動して下さい。ページを移動すると履歴が残り「Back」ボタンや「Forward」ボタンで履歴を前後に移動できます。

閲覧履歴をクリア

では履歴をクリアします。「Clear History」ボタンをクリックすると履歴がクリアされて「Back」ボタンや「Forward」ボタンをクリックしても前のページや後のページに移動できなくなります。

閲覧履歴をクリア

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。