1

What could be causing this exception?

E/AndroidRuntime(16901): FATAL EXCEPTION: main
E/AndroidRuntime(16901): Process: com.borqs.karbonn.music, PID: 16901
E/AndroidRuntime(16901): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
E/AndroidRuntime(16901): at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
E/AndroidRuntime(16901): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341)
E/AndroidRuntime(16901): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:609)
E/AndroidRuntime(16901): at android.app.BackStackRecord.commit(BackStackRecord.java:587)
E/AndroidRuntime(16901): at com.borqs.music.MusicHubMainActivity.onNavigationItemSelected(MusicHubMainActivity.java:1167)
E/AndroidRuntime(16901): at com.android.internal.widget.ActionBarView$1.onItemSelected(ActionBarView.java:148)
E/AndroidRuntime(16901): at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
E/AndroidRuntime(16901): at android.widget.AdapterView.access$200(AdapterView.java:48)
E/AndroidRuntime(16901): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
E/AndroidRuntime(16901): at android.os.Handler.handleCallback(Handler.java:808)
E/AndroidRuntime(16901): at android.os.Handler.dispatchMessage(Handler.java:103)
E/AndroidRuntime(16901): at android.os.Looper.loop(Looper.java:193)
E/AndroidRuntime(16901): at android.app.ActivityThread.main(ActivityThread.java:5299)
E/AndroidRuntime(16901): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(16901): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(16901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
E/AndroidRuntime(16901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)^M
E/AndroidRuntime(16901): at dalvik.system.NativeStart.main(Native Method)

This is my onSave instance code

@Override
public void onSaveInstanceState(Bundle state)
{
    super.onSaveInstanceState(state);
    saveInstance =true;
    if (state == null)
    {
        FragmentManager mFragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        MediaPlaybackFragment fragment = new MediaPlaybackFragment();
        fragmentTransaction.add(R.id.content_frame, fragment);
        fragmentTransaction.commit();
    } else {
        state.putInt("position", mPostion);
        selectedTab = tabHost.getCurrentTab();
        state.putInt("tabhostselected", selectedTab);
        state.putBoolean("isSaveInstance", saveInstance);
        // state.putBoolean("draweropened", mDrawergarment.isDrawerOpened());
    }
}

This is my on navigation code

@Override
public boolean onNavigationItemSelected(int position, long itemId) {
    Fragment newFragment = null;
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    newFragment = new MediaPlaybackFragment();
    ft.replace(R.id.content_frame, newFragment, "Music");
    ft.commit();
}

Any help would be much appreciated, if you need more information please ask.
8
  • And mention line MusicHubMainActivity.java:1167 in it. Commented Oct 29, 2014 at 6:34
  • what API level you are using? Commented Oct 29, 2014 at 6:35
  • This might be useful? Commented Oct 29, 2014 at 6:36
  • line no 1167 : ft.commit(); Commented Oct 29, 2014 at 6:39
  • @ken Y-N the link which is pasted deals with support library v4 but iam not using any v4 libraries Commented Oct 29, 2014 at 7:11

1 Answer 1

2

Change

fragmentTransaction.commit();

to

fragmentTransaction.commitAllowingStateLoss();

Probably you commit your transaction somewhere else in your code - you should use commitAllowingStateLoss() there aswell. But i would recommend you to rewrite your code to avoid commit after onSaveInstanceState()

Sign up to request clarification or add additional context in comments.

5 Comments

@ localhost but as per documentation Like commit() but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.
@ localhost what do u say for this
@zzz, sorry, but i didn't get your question:)
@ localhost as per documentation of "commitAllowingStateLoss()" it looks like we cant use this "commitAllowingStateLoss" there is chance of loosing the commit
@zzz, u won't loose a commit, you could loose state (meaning you could not operate backstack for example), but transaction will be commited and fragments will be replaced or added/deleted. Just try it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.