如何在Android的WebView中保存最后访问的URL?

huangapple go评论72阅读模式
英文:

How to save last visited url in android webview?

问题

我正在编写一个非常简单的Web视图应用程序,用于向特定类型的用户显示来自Firebase实时数据库的网页。一切都正常,但在关闭应用程序时,用户会被发送回原始的“起始点”页面。我听说你需要使用SharedPreferences来解决这个问题,但如何将它与Firebase关联起来,使一切都正常工作呢?以下是代码示例:

public class TargetActivty extends AppCompatActivity {
    private WebView webView;
    private TextView msgURL;
    private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference reference = firebaseDatabase.getReference();
    private DatabaseReference childReference = reference.child("url");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_target);
        webView = findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
    }

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

    @Override
    protected void onStart() {
        super.onStart();
        childReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String message = dataSnapshot.getValue(String.class);
                webView.loadUrl(message);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }
}

请注意,这段代码主要涉及Firebase数据库和WebView的使用。要解决在应用程序关闭后用户被发送回原始页面的问题,您可以使用SharedPreferences来存储用户的当前位置(URL),然后在应用程序重新打开时将用户重定向到该位置。这需要在适当的地方添加SharedPreferences代码。

英文:

So im writing a very simple web-view app that shows certain types of users a web-page that I get from Firebase Realtime Database. Everything works just fine but upon closing an app, user gets sent back to the original "starting point" page. I heard you have to use SharedPreferences to solve a problem but how can I tie it up with firebase so everything would work fine? Attaching the code

public class TargetActivty extends AppCompatActivity {
private WebView webView;
private TextView msgURL;
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
private DatabaseReference reference = firebaseDatabase.getReference();
private DatabaseReference childReference = reference.child("url");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
@Override
protected void onStart() {
super.onStart();
childReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String message = dataSnapshot.getValue(String.class);
webView.loadUrl(message);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}}

答案1

得分: 1

使用以下代码:

webView.setWebViewClient(new MyWebViewClient());

class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        // 将最后访问的URL保存到共享首选项
        saveUrl(url);
    }
}
英文:

Use this code :

webView.setWebViewClient(new MyWebViewClient());
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//Save the last visited URL to shared preference
saveUrl(url);
}
}

huangapple
  • 本文由 发表于 2020年10月7日 18:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64242094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定