Android SavedInstanceState 在片段中始终为 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20550016/
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 03:02:07  来源:igfitidea点击:

SavedInstanceState is always null in fragment

androidfragmentbundle

提问by TommyNecessary

I have a fragment attached to the activity using XML (and setContentView() in activity). A have a problem because I have very dynamic views in my fragment, so when orientation changes I must restore all states of views.

我有一个使用 XML(和 setContentView() 在活动中)附加到活动的片段。A 有一个问题,因为我的片段中有非常动态的视图,所以当方向改变时,我必须恢复视图的所有状态。

I have problem because I'm using something like that:

我有问题,因为我正在使用类似的东西:

public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("restore", true);
        outState.putInt("nAndroids", 2);
   }

But after orientation change when methods with param Bundle savedInstanceState are called (like onCreateView etc) my savedInstanceState is always null.

但是在调用带有参数 Bundle savedInstanceState 的方法(如 onCreateView 等)时,方向更改后,我的 savedInstanceState 始终为空。

I'm not a noob in the Android but now I'm very angry because of this problem...

我不是 Android 的菜鸟,但现在我因为这个问题而非常生气......

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (savedInstanceState == null) {
        //smth
    } else {
        // smthelse THIS IS NEVER REACHED BECAUSE BUNDLE IS ALWAYS NULL
    }

    getListView().setDivider(getResources().getDrawable(R.drawable.list_divider));
}

回答by TommyNecessary

All the problem was in that I don't declare android:id for the fragment in XML. Android needs ID or TAG to recognize stored fragment and reproduce all elements in it. So guys, remember - every instance of fragment needs unique id or tag!

所有的问题都在于我没有为 XML 中的片段声明 android:id 。Android 需要 ID 或 TAG 来识别存储的片段并重现其中的所有元素。所以伙计们,请记住 - 每个片段实例都需要唯一的 id 或标签!

Also, when setRetainInstance(true) is declared then bundle should always return null.

此外,当声明 setRetainInstance(true) 时,bundle 应始终返回 null。

回答by Arun Yogannadan

I had a similar problem where I was always getting savedInstanceState as null inspite of supplying the bundle to the Fragment.

我有一个类似的问题,尽管将包提供给 Fragment,但我总是将 savedInstanceState 设为 null。

The only solution that worked for me was to do

对我有用的唯一解决方案是做

myFragment.setArguments(bundle) 

with my bundle from the Activity and do a

用我的活动包做一个

Bundle bundle = this.getArguments();

in onCreateView of the fragment.

在片段的 onCreateView 中。

Hope this helps someone else.

希望这对其他人有帮助。

回答by Rahul Gupta

For Fragment :-

对于片段:-

use this for save state of fragment on orientation.

onCreate(Bundle save)
{
   super.onCreate(save);
   setRetainInstance(true);
}

See this tutorial :- http://techbandhu.wordpress.com/2013/07/02/android-headless-fragment/

请参阅本教程:- http://techbandhu.wordpress.com/2013/07/02/android-headless-fragment/

For Activity:-

对于活动:-

When you start your application, in onCreate, your bundle object is null, so you have to put a check like below and when you rotate your screen then onSaveInstanceis called and your bundle object is initialized

当您启动应用程序时onCreate,您的包对象为空,因此您必须进行如下检查,然后在旋转屏幕时onSaveInstance调用并初始化您的包对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    if (savedInstanceState != null) {
         boolean t = outState.getBoolean("restore"); 
         int s = outState.getInt("nAndroids");
    }
}

回答by AmirHossein Manian

First you should put your data, then call super.onSaveInstanceState(outState);

首先你应该把你的数据,然后调用 super.onSaveInstanceState(outState);

public void onSaveInstanceState(Bundle outState) {

    outState.putBoolean("restore", true);
    outState.putInt("nAndroids", 2);
    super.onSaveInstanceState(outState);
}

And be sure that activity has not nohistoryproperty in AndroidManifest.xmlor set it to false.

并确保活动没有nohistory属性 inAndroidManifest.xml或将其设置为false

        <activity
        android:noHistory="false">

回答by user3879095

Ok I know this is an old post but I couldn't find the right answer for me here nor many other places, so I am posting how I fixed my case.

好的,我知道这是一篇旧帖子,但我在这里或许多其他地方都找不到适合我的答案,所以我发布了我如何解决我的案例。

So My Fragment is inside an Activity. And I originally tried to save Bundle only in Fragment and retrieve it at onCreateView. However that was the problem.

所以我的片段在一个活动中。我最初尝试只在 Fragment 中保存 Bundle 并在 onCreateView 中检索它。然而这就是问题所在。

I fixed this by initiating myFragment object in activity and put that object to activity Bundle at onSaveInstanceState(). Then retrieved it at onRestoreInstanceState(). I used getSupportFragmentManager().putFragment/getFragment. Then the savedInstanceState in fragment was no longer null.

我通过在活动中启动 myFragment 对象并在 onSaveInstanceState() 处将该对象放入活动 Bundle 来解决此问题。然后在 onRestoreInstanceState() 中检索它。我使用了 getSupportFragmentManager().putFragment/getFragment。然后片段中的savedInstanceState 不再为空。