天天看點

Fragment中使用ViewFlipper在嚴格模式下抛出android.app.IntentReceiverLeaked

今天遇到一個bug:IntentReceiverLeaked, 是在嚴格模式下報的:

12-20 01:04:17.503 E/StrictMode(14464): android.app.IntentReceiverLeaked: Activity com.video.test.DetailActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
12-20 01:04:17.503 E/StrictMode(14464): 	at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:818)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:603)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1404)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.app.ContextImpl.registerReceiver(ContextImpl.java:1384)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.app.ContextImpl.registerReceiver(ContextImpl.java:1378)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:446)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.widget.ViewFlipper.onAttachedToWindow(ViewFlipper.java:93)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.view.View.dispatchAttachedToWindow(View.java:12125)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2453)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.view.ViewGroup.addViewInner(ViewGroup.java:3548)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.view.ViewGroup.addView(ViewGroup.java:3380)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.view.ViewGroup.addView(ViewGroup.java:3325)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.view.ViewGroup.addView(ViewGroup.java:3301)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:922)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.os.Handler.handleCallback(Handler.java:730)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.os.Handler.dispatchMessage(Handler.java:92)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.os.Looper.loop(Looper.java:137)
12-20 01:04:17.503 E/StrictMode(14464): 	at android.app.ActivityThread.main(ActivityThread.java:5265)
12-20 01:04:17.503 E/StrictMode(14464): 	at java.lang.reflect.Method.invokeNative(Native Method)
12-20 01:04:17.503 E/StrictMode(14464): 	at java.lang.reflect.Method.invoke(Method.java:525)
12-20 01:04:17.503 E/StrictMode(14464): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:760)
12-20 01:04:17.503 E/StrictMode(14464): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-20 01:04:17.503 E/StrictMode(14464): 	at dalvik.system.NativeStart.main(Native Method)
           

開始以為是Activity中的BroadcastReceiver沒有調用unregisterReceiver,排查了代碼發現都正常調用了。那應該是其他的問題了, 我試着檢索了ViewFlipper,發現DetailActivity中有個Fragment用到了ViewFlipper, 網上查了下資料:

(http://www.eoeandroid.com/thread-159069-1-1.html)

大多是說重載onDetachedFromWindow,并在其中加入stopFlipping;或許我的情況與之不同,依然無效;

解決:重載Activity的onDetachedFromWindow在其中加入

if(mViewFlipper != null){
    mViewFlipper .onDetachedFromWindow();
}
           

即可

由于我使用的是Fragment, 後來在重載Fragment的onDetach:

@Override
    public void onDetach() {
        super.onDetach();
        if(viewFlipper!=null){
            //防止android.app.IntentReceiverLeaked
            viewFlipper.stopFlipping();
        }
    }
           

發現OK了。