0
我想,当用户离开应用救我webView
状态其页面滚动位置和恢复他们当用户再次打开该应用程序。因此,用户可以继续阅读已恢复的webview内容,并向下滚动到恢复的位置。还原webview滚动位置?
下面是我使用的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
webView = (WebView)this.findViewById(R.id.webView);
if (savedInstanceState != null) {
int positionY = Math.round(scrollPos);
webView.scrollTo(0, positionY);
} esle {
//do something
}
函数计算的位置:
private int calculateProgression(WebView content) {
int positionTopView = content.getTop();
int contentHeight = content.getContentHeight();
int currentScrollPosition = content.getScrollY();
int percentWebview = (currentScrollPosition - positionTopView)/contentHeight;
return percentWebview;
}
保存/恢复功能:
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("position", calculateProgression(webView));
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
scrollPos = savedInstanceState.getFloat("position");
}
似乎是一个很好的方法..我会尝试它:) –
谢谢你:) :) –
抱歉,但它并没有帮助,因为当用户从最近的应用程序中删除应用程序它不会调用onDestroy方法!!!!! –