【问题标题】:Google PlusOneButton leaks a contextGoogle PlusOneButton 泄露了上下文
【发布时间】:2016-09-25 15:01:13
【问题描述】:

我已经开始在我的 Android 应用上跟踪内存泄漏,并且感谢出色的 LeakCanary 库,我发现我的一些包含 GooglePlusButton 的活动正在泄漏上下文。

不幸的是,我没有找到避免它的解决方法。正如old bug report 中所述,初始化方法隐藏了它需要上下文的事实。此外,它需要一个 Activity 上下文; Android Studio 反编译器显示它(错误报告者也告诉它):

public void initialize(String var1, int var2) {
   zzab.zza(this.getContext() instanceof Activity, "To use this method, the PlusOneButton must be placed in an Activity. Use initialize(String, OnPlusOneClickListener).");

有没有比完全去掉 +1 按钮更好的解决方法?

注意:我使用的是 Google Play Services 9.6.0,但这个问题也出现在以前的版本上。

【问题讨论】:

    标签: android memory-leaks google-play-services google-plus-one leakcanary


    【解决方案1】:

    正如错误消息中所说,如果您使用的 G+1 按钮不在活动中(例如在应用栏菜单中),则需要通过调用方法来初始化它initialize(String, OnPlusOneClickListener)。

    使用 URL 更新 +1 按钮。大多数应用程序在每次按钮处于焦点时调用此方法(例如,在 Activity onResume 方法中)。要使用此方法,必须将 PlusOneButton 放置在 Activity 中。 否则使用 initialize(String, OnPlusOneClickListener)

    更多信息在这里: https://developers.google.com/android/reference/com/google/android/gms/plus/PlusOneButton

    【讨论】:

    • 感谢您的评论,但我的问题是在活动中使用 G+1 按钮;导致上下文泄漏。
    【解决方案2】:

    上一个答案为我指明了一个新的方向。我最终通过动态创建 G+1 按钮并为构造函数提供应用程序上下文来管理它:

    mPlusOneButton =  new PlusOneButton(getApplicationContext());
    parent.addView(mPlusOneButton);
    ...
    mPlusOneButton.initialize(..., new PlusOneButton.OnPlusOneClickListener() {
            @Override
            public void onPlusOneClick(Intent intent) {...}
          });
    

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 2012-07-03
      • 2014-03-08
      • 2018-01-26
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      相关资源
      最近更新 更多