开关选项卡onNewIntent导致IllegalStateException

如果我尝试切换onNewIntent方法中的导航选项卡,我的FragmentActivity(singleTop)给我IllegalStateException。

更具体地说,我的应用程序使用SherlockActionBar三个选项卡,当接收到推送通知(和意图被调用),如果应用程序暂停在另一个选项卡上,当我收到意图(在onNewIntent)我改变该选项卡(因此片段)与bar.setSelectedNavigationItem()第三个选项卡,这是我的问题。 如果应用在第三个标签上被暂停,则不会发生exception情况。

码:

@Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); Bundle bundle = intent.getExtras(); if (bundle != null) { bar.setSelectedNavigationItem(Utils.ORDER_STATUS_TAB_ID); } else { } } 

推送通知意图:

  Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.putExtra("orderUpdate", new Gson().toJson(orderUpdate)); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.contentIntent = contentIntent; 

TabListener方法(在堆栈跟踪中的第56行的注释)

 @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { ft = activity.getSupportFragmentManager().beginTransaction(); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); if (mFragment == null) { mFragment = Fragment .instantiate(activity, mClass.getName(), mArgs); ft.add(android.R.id.content, mFragment, tag); ft.commit(); } else { ft.attach(mFragment); ft.commit(); // line 56 } 

详细的例外情况:

 07-12 20:06:40.959: E/AndroidRuntime(8639): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 07-12 20:06:40.959: E/AndroidRuntime(8639): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1299) 07-12 20:06:40.959: E/AndroidRuntime(8639): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1310) 07-12 20:06:40.959: E/AndroidRuntime(8639): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541) 07-12 20:06:40.959: E/AndroidRuntime(8639): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525) 07-12 20:06:40.959: E/AndroidRuntime(8639): at com.wizche.ui.MyTabListener.onTabSelected(MyTabListener.java:56) 07-12 20:06:40.959: E/AndroidRuntime(8639): at com.actionbarsherlock.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:526) 07-12 20:06:40.959: E/AndroidRuntime(8639): at com.actionbarsherlock.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:317) 07-12 20:06:40.959: E/AndroidRuntime(8639): at com.wizche.MainActivity.onNewIntent(MainActivity.java:205) 

Solutions Collecting From Web of "开关选项卡onNewIntent导致IllegalStateException"

无论如何,我find了一个这样的问题的解决办法。 我只是在onResume而不是onNewIntent中切换选项卡:

  @Override public void onResume() { super.onResume(); if(switchToTab){ bar.setSelectedNavigationItem(Utils.ORDER_STATUS_TAB_ID); switchToTab = false; } } 

而在onNewIntent()我只是设置switchToTab = true。 我希望有人会有更好的解决scheme。

我想你不应该在onTabSelected方法中调用提交。 它已经由框架完成了。

哦,并使用您收到的交易,不要创build一个新的。

  public void onTabSelected(Tab tab, FragmentTransaction ft) { //remove the first line //ft = activity.getSupportFragmentManager().beginTransaction(); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); if (mFragment == null) { mFragment = Fragment .instantiate(activity, mClass.getName(), mArgs); ft.add(android.R.id.content, mFragment, tag); //not sure about this one ft.commit(); } else { ft.attach(mFragment); //not sure about this one neither ft.commit(); // line 56 }