小编Vid*_*erg的帖子

getSupportFragmentManager()导致java.lang.IllegalStateException:onSaveInstanceState后无法执行此操作

我试图找出原因:

getSupportFragmentManager().beginTransaction().commit();
Run Code Online (Sandbox Code Playgroud)

失败了

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
Run Code Online (Sandbox Code Playgroud)

在一个非常基本的FragmentActivity类中.

所以这是我的用例(这将是一些伪代码,而不是一个完整的例子,抱歉):我有一个FragmentActivity与内部AsyncTask类.大概是这样的:

public class HelloWorld extends FragmentActivity {
    showFragment(Fragment fragment, String name) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, fragment, name).commit();
    }

    private class SlowFragmentShow extends AsyncTask<Context, String, Void> {
        protected Void doInBackground(Context... contexts) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                /* meh */
            }
        }

        protected void onPostExecute(Void nothing) {
            showFragment(new MyFragment(), "myFragment");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

或者基本上,在启动应用程序10秒后,它将显示另一个片段.听起来很简单吧?这似乎也很有效,直到我决定旋转手机.当我这样做时,应用程序将在使用"无法执行此操作..."调用"getSupportFragmentManager()..."时崩溃.

android android-asynctask illegalstateexception android-fragmentactivity android-support-library

5
推荐指数
1
解决办法
7189
查看次数