web视图没有恢复状态,它被打死后用户 [英] Webview is not restoring the state after it was killed by the user

查看:141
本文介绍了web视图没有恢复状态,它被打死后用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在一个麻烦几天的时间来理解这个问题。基本上我有一个 web视图它加载一个网页,第一页是登录和登录后的内容如下。

i've been in a trouble for some days to understand this issue. Basically i have a Webview which loads a website, with the first page being the login and the content follows after the login.

每一个用户验证通过网站,它自制。因此,有什么可以保存在共享preference 为例。我只是在使用url在这种情况下。

Every user validation is made through the site it self. So there is nothing to be saved in a SharedPreference for example. Im only using the url in this scenario.

这样的onCreate杀死我的应用程序后,web视图没有恢复previous状态就被打死了。

so onCreate after killing my app , the webview is not restoring the previous state before it was killed.

我想它,因为savedinstancestate即将空,并再次加载URL中的应用程序被杀害后。

I suppose its because the savedinstancestate is coming null, and is loading the url again after the app being killed.

我有会话Cookie和其他的东西。

I have the session cookies and other stuff.

我想知道,如果有人有一些建议。

i was wondering if someone has some suggestion.

p.s.I'm新的Andr​​oid系统。

p.s.I'm new in Android.

推荐答案

,你不可能指望的onCreate把你的WebView回到那个是开放的最后一页。有迹象表明,你需要做两件事情:

As you yourself pointed out since the saved state is null, you cannot possibly expect onCreate to take your webview back to the last page that was open. There are two things that you need to do:

请确保您的登录页面,这是你的第一个页面重定向到相应和内容页它检测到用户已经登录的时候。既然你已经使用Cookie,这是微不足道的。

Make sure that your login page, which is your first page redirects to a relevent content page when it detects that the user has already logged in. Since you are already using cookies this is trivial.

然后在乘坐的onPause方法来保存web视图的当前的URL。

Then over ride the onPause method to save the current url of the webView.

@Override
protected void onPause() {
    super.onPause();
    SharedPreferences prefs = context.getApplicationContext().
            getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
    Editor edit = prefs.edit();
    edit.put("lastUrl",webView.getUrl());
    edit.commit();   // can use edit.apply() but in this case commit is better
}

然后你可以阅读的onCreate方法这个属性,并根据需要加载的URL。如果preference定义加载它,如果没有加载登录页面(这应该重定向到第一个内容页面,如果已经登录)

Then you can read this property on the onCreate method and load the url as needed. If the preference is defined load it, if not load the login page (which should redirect to first content page if already logged in)

更新 这里是你的onResume可能是什么样子。还增加了一行到上述的onPause()方法

UPDATE here's what your onResume might look like. Also added one line to the above onPause() method.

@Override
protected void onResume() {
    super.onResume();
    if(webView != null) {
        SharedPreferences prefs = context.getApplicationContext().
            getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
        String s = prefs.getString("lastUrl","");
        if(!s.equals("")) {
             webView.loadUrl(s);
        }
    }
}

这篇关于web视图没有恢复状态,它被打死后用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆