7

如果我尝试在 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)
4

2 回答 2

11

我找到了解决这个问题的方法,反正有点难看。我只是在 onResume 而不是 onNewIntent 中切换选项卡:

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

在 onNewIntent() 中,我只设置了 switchToTab = true。我希望有人会提出更好的解决方案。

于 2012-07-12T19:04:29.767 回答
0

我认为你不应该在 onTabSelected 方法中调用 commit。它已经由框架完成。

哦,使用你收到的交易,不要创建一个新的。

 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
    }
于 2012-07-12T18:25:34.233 回答