gpt4 book ai didi

android - popBackStack 中的非法状态异常

转载 作者:行者123 更新时间:2023-11-29 19:07:05 61 4
gpt4 key购买 nike

在我的应用程序中,我使用的是 BottomBar 菜单。快速选择菜单时,我会收到有关 crashlytics 的崩溃报告(仅在某些设备中)。这是堆栈跟踪

Non-fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1842) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1860) at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:781) at com.beco.ibeco.app.MainActivity$1.onMenuTabSelected(MainActivity.java:87) at com.beco.ibeco.app.views.bottombar.BottomBar.notifyMenuListener(BottomBar.java:1239) at com.beco.ibeco.app.views.bottombar.BottomBar.updateSelectedTab(BottomBar.java:1199) at com.beco.ibeco.app.views.bottombar.BottomBar.handleClick(BottomBar.java:1161) at com.beco.ibeco.app.views.bottombar.BottomBar.onClick(BottomBar.java:1149) at android.view.View.performClick(View.java:5076) at android.view.View$PerformClick.run(View.java:20279) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5930) at java.lang.reflect.Method.invoke(Method.java) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
And here is the code

protected void onCreate(Bundle savedInstanceState) {
mActive = true;
super.onCreate(savedInstanceState);
initBottomBar(savedInstanceState);
Intent intent = getIntent();
}
private void initBottomBar(Bundle savedInstanceState) {
mBottomBar = BottomBar.attach(this, savedInstanceState);
mBottomBar.useFixedMode();
mBottomBar.noTabletGoodness();
mBottomBar.setItemsFromMenu(R.menu.bottombar_menu, new OnMenuTabClickListener() {
@Override
public void onMenuTabSelected(@IdRes int menuItemId) {

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);//line 87

switch (menuItemId) {

case R.id.bottomBarHome:
showStoreList();
break;

case R.id.bottomBarMap:
showMap();
break;

case R.id.bottomBarDeal:
showDeals();
break;

case R.id.bottomBarSettings:
showSettings();
break;

case R.id.bottomBarNotification:
if (mNotificationBadge != null) {
mNotificationBadge.hide();
Beco.getApp().clearBadge();
}
showNotifications();
break;
}
}

@Override
public void onMenuTabReSelected(@IdRes int menuItemId) {
switch (menuItemId) {
case R.id.bottomBarHome:
if (mCurrentTab != HOME)
showStoreList();
break;

case R.id.bottomBarMap:
if (mCurrentTab != MAP)
showMap();
break;

case R.id.bottomBarDeal:
if (mCurrentTab != DEAL)
showDeals();
break;

case R.id.bottomBarNotification:
if(mCurrentTab != NOTIFICATION) {
if (mNotificationBadge != null) {
mNotificationBadge.hide();
mNotificationBadge.setDealItemCount(0);
Beco.getApp().clearBadge();
}
showNotifications();
}
break;

case R.id.bottomBarSettings:
if (mCurrentTab != SETTINGS)
showSettings();
break;
}
}
});

mNotificationBadge = mBottomBar.makeBadgeForTabAt(3, R.color.beco_badge_color, Beco.getApp().getBadgeCount());
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mBottomBar.onSaveInstanceState(outState);
}

如何解决这个问题?

最佳答案

创建一个 bool 值并检查 Activity 是否不会暂停

@Override
public void onResume() {
super.onResume();
mIsResumed = true;
}

@Override
public void onPause() {
mIsResumed = false;
super.onPause();
}

然后在加载 fragment 检查时

if(mIsResumed){
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);//line 8
}

关于android - popBackStack 中的非法状态异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46790014/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com