为什么我的活动泄漏? [英] Why does my Activity leak?

查看:422
本文介绍了为什么我的活动泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有IntentFilter的和一个BroadcastReceiver一个活动,我在登记他们我的的onCreate()

I have an Activity that has an IntentFilter and a BroadcastReceiver, and I register them in my onCreate().

IntentFilter filter = new IntentFilter(ACTION_RCV_MESSAGE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MessageReceiver();
registerReceiver(receiver, filter);

但在结束我的应用程序,LogCat中说:

But upon ending my app, Logcat says:

一月7日至3日:38:19.567:ERROR / ActivityThread(304):android.app.IntentReceiverLeaked:活动com.intentservicetest5.IntentServiceTest5Activity渗漏,最初这里注册IntentReceiver com.intentservicetest5.IntentServiceTest5Activity$MessageReceiver@44efe8a8。你缺少unregisterReceiver()的调用?

07-03 01:38:19.567: ERROR/ActivityThread(304): android.app.IntentReceiverLeaked: Activity com.intentservicetest5.IntentServiceTest5Activity has leaked IntentReceiver com.intentservicetest5.IntentServiceTest5Activity$MessageReceiver@44efe8a8 that was originally registered here. Are you missing a call to unregisterReceiver()?

不过,我有

public void onDestroy(Bundle savedInstanceState){
    unregisterReceiver(receiver);
}

为什么我的活动仍然漏水,当我做电 unregisterReceiver()

推荐答案

%的的活动的生命周期,你的应用程序是killable后的任何时间的onPause()在大多数设备上,以及(低于Android 3.0的/ API级12中的任何设备)在任何设备上后,的onStop()的任何时间。这意味着的onDestroy()不能保证被调用。在活动的最佳场所注册和注销的广播,那么,是 onResume()的onPause(),分别为:

Per the Activity Lifecycle, your app is killable any time after onPause() on most devices (any device with less than Android 3.0 / API level 12), and any time after onStop() on any device. That means onDestroy() is not guaranteed to be called. The best place in an Activity to register and unregister for broadcasts, then, is onResume() and onPause(), respectively:

@Override
protected void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter(ACTION_RCV_MESSAGE);
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    receiver = new MessageReceiver();
    registerReceiver(receiver, filter);
}

@Override
protected void onPause() {
    super.onPause();

    unregisterReceiver(receiver);
}

请注意,由于@VipalShah指出的那样,你使用了错误的签名的onDestroy()。这就是为什么 unregisterReceiver()没有被调用。使用 @覆盖将帮助您捕捉这类问题。

Note that as @VipalShah pointed out, you were using the wrong signature for onDestroy(). This is why unregisterReceiver() wasn't being called. Using @Override will help you catch this kind of problem.

另外请注意,的onPause之间的对称() onResume()是故意的。如果你看一下链接上述活动的生命周期中,您将看到 onResume()是初次启动时调用,从暂停恢复不只是当。所以,你只需要为广播在此注册,而不是在的onCreate()

Also note that the symmetry between onPause() and onResume() is intentional. If you look at the Activity lifecycle article linked to above, you'll see that onResume() is called during the initial start-up, not just when resuming from pause. So you only need to register for the broadcast here, not in onCreate().

最后,请注意,我用你的code为简单起见。在现实中,你可能要设置你的的onCreate的IntentFilter和的messageReceiver(),然后就叫 registerReceiver() onResume()

Finally, note that I used your code for simplicity. In reality, you'll probably want to set up your IntentFilter and MessageReceiver in onCreate(), and then just call registerReceiver() in onResume().

更新:还有一个重要的注意事项。按照实施生命周期回调的:

Update: One more important note. As per Implementing the lifecycle callbacks:

您实现这些生命周期方法必须始终做任何工作之前调用父类的实现。

Your implementation of these lifecycle methods must always call the superclass implementation before doing any work.

很多人在这里被展示在调用 super.onWhatever()在方法结束而不是开始。

Many people here were showing the call to super.onWhatever() at the end of the method rather than at the beginning.

这篇关于为什么我的活动泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆