Switch tab onNewIntent throws IllegalStateException

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

In particular, my application uses a SherlockActionBar with three tabs, one tab is updated when a push notification is received (and the intent is called), if the application was suspended on another tab, when I get the intention (in onNewIntent) I change the tab (and therefore the fragment ) to the third tab using bar.setSelectedNavigationItem (), and this causes me a problem. If the application was suspended on the third tab, an exception does not occur.

the 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 purpose of the push notification:

  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 method (with comment line 56 on the stack)

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

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) 
+7
source share
2 answers

I found a fix for this, sort of ugly. I just switch the tab to onResume instead of onNewIntent:

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

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

+10
source

I think you should not call commit in the onTabSelected method. This has already been done by wireframe.

Oh, and use the resulting transaction, do not create a new one.

  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 } 
0
source

All Articles