这是代码
FragmentManager fm = getSupportFragmentManager();
int count = fm.getBackStackEntryCount();
for (int i = 0; i < count; ++i) {
fm.popBackStackImmediate();
}
此代码在所有情况下都能正常运行。但是当从广播接收器调用时,我们正在获得非法状态异常。
分享logcat
java.lang.RuntimeException:
at android.app.LoadedApk$ReceiverDispatcher$Args.run
(LoadedApk.java:894)
at android.os.Handler.handleCallback (Handler.java:739)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5484)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException:
at android.support.v4.app.FragmentManagerImpl.checkStateLoss
(FragmentManager.java:1842)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate
(FragmentManager.java:775)
at com.app.sample.BaseContentFragmentHolderActivity.startFragment
(BaseContentFragmentHolderActivity.java:58)
at app.sample.SampleActivity.showDashboardFragment
(SampleActivity.java:732)
at com.app.sample.SampleActivity.onMotionDetected
(SampleActivity.java:725)
at com.app.sample.SampleActivity$3.onReceive
(SampleActivity.java:261)
at android.app.LoadedApk$ReceiverDispatcher$Args.run
(LoadedApk.java:884)
对此有何想法?
答案 0 :(得分:0)
如果应用程序在后台,异常会上升。实现逻辑以确保应用程序在执行这些代码时处于前台。