Android application crashes if backgrounded while holding navigation drawer item

时间:2015-07-28 23:00:08

标签: android crash navigation-drawer

I have an application that uses the navigation drawer design pattern as per the Android Developer website implementation. Basically each item in the list opens a fragment (as per the normal paradigm). I have found that if I hold down one of the navigation drawer items while the application is backgrounded (which can be done by hitting the home key or when a phone call comes in) the application crashes.

Notably, the crash occurs when I remove my finger from the screen and so you can hold your finger on the screen indefinitely and it won't crash until the finger is lifted.

Stacktrace is as follows,... so it seems I might be able to answer my own question in a moment - thanks for the (sort-of-obvious-now) prompt :-)

07-29 09:11:03.182    5044-5044/au.com.xxx.yyy E/AndroidRuntime﹕ FATALEXCEPTION: main
Process: au.com.xxx.yyy, PID: 5044
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
        at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341)
        at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)
        at android.app.BackStackRecord.commit(BackStackRecord.java:575)
        at au.com.xxx.yyy.userInterface.MainActivity.onNavigationDrawerItemSelected(MainActivity.java:264)
        at au.com.xxx.yyy.userInterface.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:235)
        at au.com.xxx.yyy.userInterface.NavigationDrawerFragment$1.onItemClick(NavigationDrawerFragment.java:104)
        at android.widget.AdapterView.performItemClick(AdapterView.java:308)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1478)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3480)
        at android.widget.AbsListView.onTouchUp(AbsListView.java:4838)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:4601)
        at android.view.View.dispatchTouchEvent(View.java:8017)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2129)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2280)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1615)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2544)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2228)
        at android.view.View.dispatchPointerEvent(View.java:8212)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4499)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4367)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3925)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3979)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3948)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4059)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3956)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4116)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3925)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3979)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3948)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3956)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3925)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6306)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6210)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6181)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6146)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6386)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:138)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:5356)

1 个答案:

答案 0 :(得分:0)

对于任何关注答案的人都非常简单(并且通过堆栈跟踪暗示 - 感谢Ashwin)。

在显示片段的标准模式中,使后退按钮工作如下:

@Html.DropDownListFor(m => m.selectedBSFlag, Model.bsFlag, new { @class =  "form-control input-sm", ng_model = "inputForm.bsFlag" })

如果您已经执行了onSaveInstanceState(...)调用,那么transaction.commit()行将抛出上面显示的异常。

因此我创建了一个标志来说明我当前是否正在处理onCaveInstanceState调用,我在onCreate和onResume中清除了该调用。然后,我使用此标志绕过onNavigationDrawerItemSelected(...)

中的所有BackStack内容

瞧!一旦你把头放在正确的位置就明显了!!