Android的严格模式下侦测到多个活动实例侵犯

Android的严格模式下侦测到多个活动实例侵犯

本文介绍了Android的严格模式下侦测到多个活动实例侵犯,但我不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code可能是太复杂,张贴在这里充分,但在这​​里的基本架构:我有两个活动的子类,每个举办的ListView 。每个的ListView 有一个自定义类,它生成自定义类的查看实例也是一个适配器。这些列表显示,在另一个线程异步产生的数据项;因为它需要知道在哪里发送更新,数据对象是操纵具有的WeakReference<>被设置为持有引用到适配器显示其内容,当他们对象初始化。当选择了第一项活动的列表中的对象,我开始与该指示它来查找该项目并显示其内容的意图次活动。然后我用的是返回按钮,关闭第二个活动,并返回到第一位。出于某种原因,当我运行这个与 StrictMode 检查已启用,后两个活动之间的切换,抱怨也有一个这种情况太多了几次反复老是死机我活动类。

Code is probably too complex to post here in full, but here is the basic schema: I have two Activity subclasses, each of which hosts a ListView. Each ListView has an adapter of a custom class, which generates View instances also of a custom class. These lists are showing data items that are generated asynchronously in another thread; as it needs to know where to send updates to, the data objects it manipulates have WeakReference<> objects that are set to hold references to the adapters displaying their contents when they are initialised. When an object in the list of the first activity is selected, I start the second activity with an intent that instructs it to look up the item and display its contents. I then use the 'back' button to close the second activity and return to the first. For some reason when I run this with StrictMode checking enabled, it always crashes after a few iterations of switching between the two activities, complaining that there are too many instances of one of my Activity classes.

我已经安排了一个堆转储将崩溃(见的Andr​​oid StrictMode之前,为了写和堆转储)。这些堆转储总是表明,有我的每个堆两项活动的1个实例的终止时间。首先,这是不是可以预期,当我最近在两者之间切换,如果是这样的话,为什么 StrictMode 抱怨呢?如果没有预期,我该怎么安排避免这种情况?检查堆转储,这两个对象都从主线程的堆栈,我好像过不具有控制任何有用程度的参考。每间也都从 android.app.ActivityThread $ ActivityClientRecord 的参考,这也是我似乎并不能够控制。

I have arranged for a heap dump to be written just prior to the crash (see Android StrictMode and heap dumps). These heap dumps always show that there is 1 instance of each of my two activities on the heap at the time of termination. First of all, is this not to be expected when I have recently switched between the two, and if that is so, why is StrictMode complaining about this? If it isn't expected, how can I arrange to avoid this? Examining the heap dump, both objects are referenced from the main thread stack, over which I don't seem to have any useful degree of control. Each also has a reference from android.app.ActivityThread$ActivityClientRecord, which I also do not seem to be able to control.

所以,基本上,任何想法如何避免这种情况?这是否实际上是重新present活动泄漏,或者是StrictMode只是过于敏感?

So, basically, any ideas how I avoid this situation? Does this actually represent an activity leak, or is StrictMode just being overly sensitive?

推荐答案

我知道这是旧的文章。只是为了家伙谁正在寻找解决方案,并解释了这个问题。

I know that this is old post. Just for guys who is looking for solution and explanation to this problem.

在的情况下有例外InstanceCountViolation它意味着有一个真正的问题,即活动泄漏。否则有可能的问题是关系到detectActivityLeaks如何检查Android SDK中实现。

In case there is InstanceCountViolation exception it means that there is a real problem that Activity leak. Otherwise there can problem which is related to how detectActivityLeaks check is implemented in Android SDK.

要确定是否这是一个问题,我可以推荐以下职位:。如果你会看到有拿着引用这次活动的对象不涉及到Android框架,那么你必须应固定你的问题。

To identify if this is a problem I can recommend the following post: Detecting leaked Activities in Android. If you will see that there are objects holding a reference to this activity which don't related to Android Framework then you have a problem which should be fixed by you.

在情况下,有拿着引用这项活动不涉及到Android框架比它意味着你有相关的detectActivityLeaks检查是如何实现的问题,遇到的任何对象。在这种情况下失败的活动解决问题,而不必关闭detectActivityLeaks,你可以简单地运行的的System.gc()的下面的示例开始活动调试配置像以前一样:

In case there are no objects holding a reference to this activity which don't related to Android Framework than it means that you encountered with the problem related to how detectActivityLeaks check is implemented. In this case to fix the problem with failed activity without turning off detectActivityLeaks you can simply run System.gc() before starting activity in debug configuration like in the following example:

 if (BuildConfig.DEBUG)
 {
     System.gc();
 }

 Intent intent = new Intent(context, SomeActivity.class);
 this.startActivity(intent);

更多信息,在此回答。

这篇关于Android的严格模式下侦测到多个活动实例侵犯,但我不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:03