getSupportFragmentManager()。putFragment问题

时间:2012-09-13 08:44:00

标签: android

我正在开发一个Android应用程序,我想保存我的片段在矢量片段中。当我在onSaveIstanceState中使用putFragment时,系统会抛出IllegalStateException

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Salviamo lo stato 


        //salvo all fragment
        for(int i=0;i<fragments.size();i++)
            getSupportFragmentManager().putFragment(outState, Integer.toString(i), fragments.get(i));

    }

这是logcat:

09-13 10:38:10.230: E/AndroidRuntime(24089): java.lang.IllegalStateException: Fragment GenericPageFragment{417716e0} is not currently in the FragmentManager
09-13 10:38:10.230: E/AndroidRuntime(24089):    at android.support.v4.app.FragmentManagerImpl.putFragment(FragmentManager.java:516)
09-13 10:38:10.230: E/AndroidRuntime(24089):    at it.intects.dashboard.droid.BaseActivity.onSaveInstanceState(BaseActivity.java:180)
09-13 10:38:10.230: E/AndroidRuntime(24089):    at android.app.Activity.performSaveInstanceState(Activity.java:1113)
09-13 10:38:10.230: E/AndroidRuntime(24089):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1188)

为什么?如何解决此错误?

1 个答案:

答案 0 :(得分:1)

调用savedInstanceState()后,您无法执行任何片段事务。

当活动返回时,活动将恢复最后附加的片段。

如果您想使用片段执行任何操作:

getSupportFragmentManager().beginTransaction().add(R.layout.container,new MyFragment(), "fragment_tag").commit();

A very good tutorial about fragments