我有以下代码:
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)
有人可以帮助我了解最新情况吗?
答案 0 :(得分:1)
检查这是否有帮助:
public void popBackStackMethod(){
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
this.getChildFragmentManager().popBackStack();
disposableLayout.setVisibility(View.VISIBLE);
}
}
答案 1 :(得分:0)
在getChildFragmentManager之前使用getActivity
,如下所示
getActivity().getChildFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer,educationalFragment,"eduFrag")
.addToBackStack("howItWorks").commit();