java - 为 BacKstack 调用 getChildFragmentManager() 时出现非法状态异常

标签 java android xml android-studio android-fragments

我有以下代码:

RelativeLayout howItWorks;
RelativeLayout howWeHelp;
RelativeLayout knowYourBloodPressure;
RelativeLayout learnAboutBirthControl;
LinearLayout disposableLayout;
LinearLayout fragmentContainer;
EducationalFragment educationalFragment;
ReactiveButton button;


@Override
public void onAttach(Context context) {
    super.onAttach(context);

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_educational, container, false);
    //View view = inflater.inflate(R.layout.fragment_ocp_get_prescription, container, false);
    if (view != null)
    {
        ButterKnife.inject(this, view);
        //Artisan.link(linkText, "www.plannedparenthood.org/health-center", "https://www.plannedparenthood.org/health-center");
    }
    howItWorks = (RelativeLayout) view.findViewById(R.id.HowItWorks);
    howWeHelp = (RelativeLayout) view.findViewById(R.id.HowWeHelp);
    knowYourBloodPressure = (RelativeLayout) view.findViewById(R.id.KnowYourBloodPressure);
    learnAboutBirthControl = (RelativeLayout) view.findViewById(R.id.LearnAboutBirthControl);
    disposableLayout =(LinearLayout)view.findViewById(R.id.disposableView);
    fragmentContainer = (LinearLayout)view.findViewById(R.id.fragmentContainer);
    button = (ReactiveButton)view.findViewById(R.id.action_continue);
    return view;
}


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


    howItWorks.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            educationalFragment = new EducationalFragment();
            disposableLayout.setVisibility(View.GONE);
            button.setVisibility(View.INVISIBLE);
            getChildFragmentManager().beginTransaction().replace(R.id.fragmentContainer,educationalFragment,"eduFrag").addToBackStack("howItWorks").commit();
            educationalFragment.setFirstScreen(1);

        }
    });
    howWeHelp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            disposableLayout.setVisibility(View.GONE);
            educationalFragment = new EducationalFragment();
            button.setVisibility(View.INVISIBLE);
            getChildFragmentManager().beginTransaction().replace(R.id.fragmentContainer,educationalFragment,"eduFrag").addToBackStack("howWeHelp").commit();
            educationalFragment.setFirstScreen(2);
        }
    });
    knowYourBloodPressure.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            educationalFragment = new EducationalFragment();
            disposableLayout.setVisibility(View.GONE);
            button.setVisibility(View.INVISIBLE);
            getChildFragmentManager().beginTransaction().replace(R.id.fragmentContainer,educationalFragment,"eduFrag").addToBackStack("knowYourBloodPressure").commit();
            educationalFragment.setFirstScreen(3);
        }
    });
    learnAboutBirthControl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            educationalFragment = new EducationalFragment();
            disposableLayout.setVisibility(View.GONE);
            button.setVisibility(View.INVISIBLE);
            getChildFragmentManager().beginTransaction().replace(R.id.fragmentContainer,educationalFragment,"eduFrag").addToBackStack("learnAboutBirthControl").commit();
            educationalFragment.setFirstScreen(4);
        }
    });
}

@Override
public void onResume() {
    super.onResume();
    if(educationalFragment!=null) {
        getChildFragmentManager().beginTransaction().remove(educationalFragment).commit();
    }
}

@OnClick(R.id.action_continue)
public void onContinue()
{
    if (listener != null)
    {
        listener.onContinueClick();
    }
}

public void popBackStackMethod(){


    this.getChildFragmentManager().popBackStack();
    disposableLayout.setVisibility(View.VISIBLE);



}

调用 popBackStack 时遇到非法状态异常:

 FATAL EXCEPTION: main
 Process: com.ppfa.stdtestca.release, PID: 8846
 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
     at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:575)
     at com.ppfa.stdtestca.categories.ui.ocp.OCPGettingPrescriptionFragment.popBackStackMethod(OCPGettingPrescriptionFragment.java:135)
     at com.ppfa.stdtestca.core.ui.MainActivity.onBackPressed(MainActivity.java:164)
     at android.app.Activity.onKeyUp(Activity.java:2725)
     at android.view.KeyEvent.dispatch(KeyEvent.java:2699)
     at android.app.Activity.dispatchKeyEvent(Activity.java:3026)
     at com.bluerock.sapphire.util.CompositeWindowCallback.dispatchKeyEvent(CompositeWindowCallback.java:49)
     at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:317)
     at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4327)
     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4298)
     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4028)
     at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4189)
     at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2365)
     at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1961)
     at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1952)
     at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2342)
     at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
     at android.os.MessageQueue.nativePollOnce(Native Method)
     at android.os.MessageQueue.next(MessageQueue.java:323)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:6077)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

有人可以帮助我了解发生了什么事吗?

最佳答案

检查这是否有帮助:

  public void popBackStackMethod(){
               if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                    this.getChildFragmentManager().popBackStack();
                    disposableLayout.setVisibility(View.VISIBLE);
                } 
    }

关于java - 为 BacKstack 调用 getChildFragmentManager() 时出现非法状态异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39605818/

相关文章:

java - 从 session 变量中删除特定变量

java - JTextArea 文本不可见且无滚动

android - Realm 在多线程场景中给出陈旧的结果

java - 如何在 XSLT 2.0 中获取当前 xml 文件名?

python - lxml unicode实体解析问题

java - 对话框仅在启动时第一次启动?

java - 如果 block 以 false 值执行,则使用 substring 和 equals 方法

Android 未定义的 getActivity 错误

c# - 使用 System.ServiceModel 在 Xamarin 中使用 WCF REST 基本身份验证服务

xml - 在 XSLT 中调用函数