ANDROID-java.lang.IllegalStateException:在onSaveInstanceState之后无法执行此操作 [英] ANDROID - java.lang.IllegalStateException: cannot perform this action after onSaveInstanceState

查看:288
本文介绍了ANDROID-java.lang.IllegalStateException:在onSaveInstanceState之后无法执行此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用finish()然后重新打开应用程序时出现上述错误.这是在onBackPressed()上管理导航的代码.

I'm getting the above error when calling finish() and then re-opening the app. Here's the code that manages navigation onBackPressed().

        android.app.FragmentManager settingsFragmentManager = getFragmentManager();
        FragmentManager fragmentManager = getSupportFragmentManager();

        if (settingsFragmentManager.getBackStackEntryCount() > 0)
            settingsFragmentManager.popBackStackImmediate();
        else if (fragmentManager.getBackStackEntryCount() > 0)
            fragmentManager.popBackStackImmediate();        

        if (fragmentManager.getBackStackEntryCount() == 0 && endIfEmpty) finish();
        if (fragmentManager.getBackStackEntryCount() == 0 && !endIfEmpty) loadView(0);
        if (fragmentManager.getBackStackEntryCount() == 0 && !userHasLoggedIn)
        {
            LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(loginBroadcastReceiver);
            finish();
        }

loadView调用方法负责根据用户选择的内容添加片段.当以0作为参数调用它时,它将添加默认片段.

The method loadView calls is in charge of adding a fragment, based on what the user selected. When it is invoked with 0 as argument, it adds the default fragment.

此方法:

    private void loadView(int position)
    {   
        FragmentManager fragmentManager = getSupportFragmentManager();
        Bundle fragmentArgs = new Bundle();

        if (drawerOpened)
            mDrawerLayout.closeDrawer(drawerHolderLayout);

        switch (position) {
        case 0:
            ProfileEditFragment activityList = new ProfileEditFragment();

            fragmentArgs.clear();
            fragmentArgs.putString(ProfileEditFragment.ARG_AGENT_ID, String.valueOf(prefClass.getAgentId()));
            fragmentArgs.putString(ProfileEditFragment.ARG_USERNAME, prefClass.getUsername());
            fragmentArgs.putString(ProfileEditFragment.ARG_PASSWORD, prefClass.getHashedPassword());
            fragmentArgs.putInt(ProfileEditFragment.ARG_OPERATION, ProfileEditFragment.LOAD_ALL_DATA);

            activityList.setArguments(fragmentArgs);
            fragmentManager.beginTransaction().add(R.id.main_activity_content, activityList).addToBackStack(null).commit();
            //activityList.retrieveActivityFromServer(ProfileEditFragment.LOAD_ALL_DATA);

            break;
        }
    }

执行此行时会发生异常:

The exception occurrs when this line is executed:

fragmentManager.beginTransaction().add(R.id.main_activity_content, activityList).addToBackStack(null).commit();

但是,首次启动应用程序时不会发生此异常.它仅在调用finish()后重新启动时显示.值得一提的是,添加的第一个片段是LoginFragment.登录过程完成后,将调用popBackstack,backstack变为空并调用loadView(0).那么,为什么在调用loadView时却崩溃并导致异常崩溃,而在添加LoginFragment时却没有崩溃呢?

However, this exception does not occurr when the app is launched for the FIRST TIME. It only appears when it is re-launched after a call to finish(). It is worth mentioning that the first fragment added is a LoginFragment. Once the login process is done, popBackstack is invoked, the backstack becomes empty and loadView(0) is called. So, why does it crash with the exception when the loadView is called but doesn't when LoginFragment is added?

谢谢.

推荐答案

在我的情况下,我将应用程序移至后台后打开了一个DialogFragment.

In my case I opened a DialogFragment after an application moved to a background.

您可以通过以下方式模拟此状态:打开请求,运行Thread.sleep(2000),快速按智能手机上的关闭电源"(或按菜单"按钮),然后按打开电源"(或打开应用程序).如果请求带来错误并显示DialogFragment,它将在日志中写入:java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState,对应的行是dialogFragment.show(supportFragmentManager, TAG).

You can emulate this state this way: open a request, run Thread.sleep(2000), quickly press "Power off" on smartphone (or press "Menu" button), then "Power on" (or open the application). If a request brings error and DialogFragment shows, it will write in log: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState, corresponding line is dialogFragment.show(supportFragmentManager, TAG).

我尝试了一种解决方案 https://stackoverflow.com/a/22414366/2914140 ,它可以正常工作,但是包含此错误.因此,再添加一张支票:

I tried a solution https://stackoverflow.com/a/22414366/2914140, it works fine, but contains this bug. So, add one more check:

if (supportFragmentManager?.isStateSaved == false) {
    dialogFragment.show(supportFragmentManager, TAG)
}

在关闭屏幕,只需打开你不会看到这个对话框(伤感地说),但您的应用程序不会崩溃后,这种情况下.

In this case after turning off a screen and turning on you simply won't see this dialog (sad to say), but your application won't crash.

更新

对不起,旧答案已经过时.参见在显示对话框时,我得到在onSaveInstanceState"之后无法执行此操作.

Sorry, the old answer is outdated. See On showing dialog i get "Can not perform this action after onSaveInstanceState".

public class CustomDialogFragment extends DialogFragment {

    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commit();
        } catch (IllegalStateException e) {
        }
    }
}

在这种情况下,将显示对话框片段.

In this case the dialog fragment will be shown.

这篇关于ANDROID-java.lang.IllegalStateException:在onSaveInstanceState之后无法执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆