如果我尝试在 onNewIntent 方法中切换导航选项卡,我的 FragmentActivity(singleTop) 会给我 IllegalStateException。
更具体地说,我的应用程序使用带有三个选项卡的 SherlockActionBar,当收到推送通知(并调用意图)时更新一个选项卡,如果应用程序暂停在另一个选项卡上,当我收到意图时(在 onNewIntent 中)我更改使用 bar.setSelectedNavigationItem() 将选项卡(以及片段)移到第三个选项卡,这导致了我的问题。如果应用程序在第三个选项卡上暂停,则不会发生异常。
代码:
@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)