2016-10-13 105 views
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"); 
    } 

回答

0

你可以保存的位置SharedPreferencesOnDestroy方法,並在中檢索它方法。

編輯:如前所述hereOnPause應該是存儲數據,而不是OnDestroy的地方。

+0

似乎是一個很好的方法..我會嘗試它:) –

+0

謝謝你:) :) –

+0

抱歉,但它並沒有幫助,因爲當用戶從最近的應用程序中刪除應用程序它不會調用onDestroy方法!!!!! –