Android 从返回堆栈恢复片段时的savedInstanceState

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11111649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:02:35  来源:igfitidea点击:

savedInstanceState when restoring fragment from back stack

androidandroid-fragmentsbundleback-stack

提问by heero

Can I use savedInstanceState()to save the state when removing a fragment, then restore the state when I pop the fragment off the back stack? When I restore the fragment from the back stack, savedInstanceState bundle is always null.

我可以savedInstanceState()在删除片段时使用保存状态,然后在从返回堆栈中弹出片段时恢复状态吗?当我从返回堆栈中恢复片段时,savedInstanceState 包始终为空。

Right now, the app flow is: fragment created -> fragment removed (added to back stack) -> fragment restored from back stack (savedInstanceState bundle is null).

现在,应用流程是:创建片段 -> 删除片段(添加到后台堆栈)-> 从后台堆栈恢复片段(savedInstanceState 包为空)。

Here is the relevant code:

这是相关的代码:

public void onActivityCreated(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    Long playlistId = bundle.getLong(Constants.PLAYLIST_ID);
    int playlistItemId = bundle.getInt(Constants.PLAYLISTITEM_ID);

    if (savedInstanceState == null) {
       selectedVideoNumber = playlistItemId;
    } else {
       selectedVideoNumber = savedInstanceState.getInt("SELECTED_VIDEO");
    }
}

public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(Constants.SELECTED_VIDEO, selectedVideoNumber);
    }

I think the problem is that onSavedInstanceState()is never called when being removed and being added to back stack. If I cant use onsavedInstanceState(), is there another way to fix this?

我认为问题在于它onSavedInstanceState()在被删除并被添加到后台堆栈时永远不会被调用。如果我不能使用 onsavedInstanceState(),还有其他方法可以解决这个问题吗?

回答by Eric

onSaveInstanceState is (unfortunately) not called in normal back-stack re-creation of a fragment. Check out http://developer.android.com/guide/components/fragments.html#Creatingand the answer on How can I maintain fragment state when added to the back stack?

onSaveInstanceState(不幸的是)在片段的正常后台堆栈重新创建中没有被调用。查看http://developer.android.com/guide/components/fragments.html#Creating和有关如何在添加到后台堆栈时保持片段状态的答案

回答by AllDayAmazing

I like to store the View I return in onCreateView as a global variable and then when I return I simply check this:

我喜欢将我在 onCreateView 中返回的视图存储为全局变量,然后当我返回时,我只需检查一下:

if(mBaseView != null) {
        // Remove the view from the parent
        ((ViewGroup)mBaseView.getParent()).removeView(mBaseView);
        // Return it
        return mBaseView;
    }

回答by bcorso

The problem is that the fragment needs to have an Idor Tagassociated with it in order for the FragmentManagerto keep track of it.

问题是片段需要有一个IdTag与之关联,以便FragmentManager跟踪它。

There are at least 3 ways to do this:

至少有 3 种方法可以做到这一点:

  1. In xml layout declare an Idfor your fragment:

    android:id=@+id/<Id>
    
  2. If your fragments container Viewhas an Id, use FragmentTransaction:

    FragmentTransaction  add (int containerViewId, Fragment fragment)
    
  3. If your fragment is not associated with any View(e.g. headless fragment), give it a Tag:

    FragmentTransaction  add (Fragment fragment, String tag)
    
  1. 在 xml 布局中Id为您的片段声明一个:

    android:id=@+id/<Id>
    
  2. 如果您的片段容器ViewId,请使用FragmentTransaction

    FragmentTransaction  add (int containerViewId, Fragment fragment)
    
  3. 如果您的片段不与任何View(例如无头片段)相关联,请给它一个Tag

    FragmentTransaction  add (Fragment fragment, String tag)
    

Also, see this SO answer.

另外,请参阅此 SO 答案。

回答by kenyee

FWIW, I hit this as well, but in my case onSaveInstanceState was called properly and I pushed in my state data when a new activity fragment was brought up on the smartphone. Same as you, the onActivityCreated was called w/ savedInstanceState always null. IMHO, I think it's a bug.

FWIW,我也打了这个,但在我的情况下,onSaveInstanceState 被正确调用,当智能手机上出现一个新的活动片段时,我推送了我的状态数据。与您一样, onActivityCreated 被调用 w/savedInstanceState 始终为空。恕我直言,我认为这是一个错误。

I worked around it by creating a static MyApplication state and putting the data there for the equivalent of "global variables"...

我通过创建一个静态的 MyApplication 状态并将数据放在那里来解决它,相当于“全局变量”......