简体   繁体   English

恢复w / o savedInstanceState后如何恢复片段状态?

[英]How to restore fragment state after resume w/o savedInstanceState?

in my app an FragmentActivity contains a simple ListFragment and I want to preserve the position to which the user has scrolled to. 在我的应用程序中, FragmentActivity包含一个简单的ListFragment ,我想保留用户滚动到的位置。 I did the following: 我做了以下事情:

private int initialScrollPosition = 0;
private int initialScrollYOffset = 0;

@Override
public void onSaveInstanceState (Bundle outState) {
    outState.putInt(SCROLL_POSITION, this.getListView().getFirstVisiblePosition());
    View firstVisibleItemView = this.getListView().getChildAt(0);
    outState.putInt(SCROLL_Y_OFFSET, (firstVisibleItemView == null) ? 0 : firstVisibleItemView.getTop());
}

@Override
public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    this.restoreState(savedInstanceState);
}

private void restoreState(Bundle savedInstanceState) {
    if(savedInstanceState != null
            && savedInstanceState.containsKey(SCROLL_POSITION)) {
        this.initialScrollPosition = savedInstanceState.getInt(SCROLL_POSITION);
    }
    if(savedInstanceState != null
            && savedInstanceState.containsKey(SCROLL_Y_OFFSET)) {
        this.initialScrollYOffset = savedInstanceState.getInt(SCROLL_Y_OFFSET);
    }
}

@Override
public void onStart () {
    super.onStart();
    // some other GUI stuff
    this.getListView().setSelectionFromTop(this.initialScrollPosition, this.initialScrollYOffset);
}

This solution works fine for things like display orientation changes or moving from one Activity of my app to the next. 此解决方案适用于显示方向更改或从我的应用程序的一个Activity移动到下一个应用程序。

But if I fire up the app, scroll a bit, go back to home, and restore the App from Recents, onActivityCreated (or any other callbacks with the savedInstanceState Bundle ) is not called and therefore the state cannot be restored. 但是,如果我启动应用程序,滚动一点,返回主页,并从最近恢复应用程序, onActivityCreated (或任何其他具有savedInstanceState Bundle回调)不会被调用,因此无法恢复状态。

Therefore I have two questions: 因此我有两个问题:

  1. What can I do to get the savedInstanceState after resuming without the onXXXCreated callbacks? 我能做些什么,以获得savedInstanceState没有恢复后onXXXCreated回调?
  2. What exactly is the lifecycle doing there. 那里的生命周期到底是什么。 I'd like to understand, why my approach fails. 我想明白,为什么我的方法失败了。

Thank you very much for any insight! 非常感谢您的任何见解!

Solution: 解:

Thx to André! 感谢安德烈! I changed my onSaveInstanceState to this: 我将onSaveInstanceState更改为:

@Override
public void onSaveInstanceState (Bundle outState) {
    this.initialScrollPosition = this.getListView().getFirstVisiblePosition();
    View firstVisibleItemView = this.getListView().getChildAt(0);
    this.initialScrollYOffset = (firstVisibleItemView == null) ? 0 : firstVisibleItemView.getTop();
    outState.putInt(SCROLL_POSITION, this.initialScrollPosition);
    outState.putInt(SCROLL_Y_OFFSET, this.initialScrollYOffset);
}

If your app is brought to front using the recent apps button, it might be that it isn't recreated, because the system didn't finish it yet. 如果您的应用程序使用最近的应用程序按钮显示在前面,则可能是它未重新创建,因为系统尚未完成。 In that case only onResume() and following callbacks are invoked. 在这种情况下,只调用onResume()和后面的回调。

Conclusion: try to restore your position in onResume() , store the positions in onPause() (for recent app switches where the activity isn't recreated), extract the positions from savedInstanceState bundle (for orientation change where the activity is recreated). 结论:尝试在onResume()恢复您的位置,将位置存储在onPause() (对于未重新创建活动的最近的应用程序切换),从savedInstanceState包中提取位置(用于重新创建活动的方向更改)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM