切换选项卡onNewIntent导致IllegalStateException

时间:2012-07-12 18:22:13

标签: java android fragment actionbarsherlock

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

更具体地说,我的应用程序使用带有三个选项卡的SherlockActionBar,一个选项卡在收到推送通知时更新(并调用intent),如果应用程序在另一个选项卡上暂停,当我收到意图时(在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)

2 个答案:

答案 0 :(得分:10)

我找到了解决这个问题的方法,无论如何都有点难看。我只是切换onResume中的tab而不是onNewIntent:

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

在onNewIntent()中我只设置了switchToTab = true。我希望有人能找到更好的解决方案。

答案 1 :(得分: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
    }