Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch tab onNewIntent cause IllegalStateException

My FragmentActivity(singleTop) is giving me IllegalStateException if I try to switch the navigation tab in the onNewIntent method.

More specifically, My application use SherlockActionBar with three tabs, one tab is updated when a push notification is received (and the intent is called), if the App was suspended on another tab, when I receive the intent (in the onNewIntent) I change the tab (and therefore the fragment) to the third tab with bar.setSelectedNavigationItem() and this is causing me the problem. If the app was suspended on the third tab, no exception occurs.

Code:

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

The push notification intent:

    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;

The TabListener method (with the comment on the line 56 in the stacktrace)

@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
        }

The detailed exception:

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)
like image 874
Wizche Avatar asked Jul 12 '12 18:07

Wizche


1 Answers

I found a fix for this, kind of ugly anyway. I just switch the tab in the onResume instead of onNewIntent:

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

And in the onNewIntent() I just set the switchToTab = true. I hope someone will come with a better solution.

like image 93
Wizche Avatar answered Oct 24 '22 22:10

Wizche