【问题标题】:Toast not showing on progressUpdate in android 6 when using getApplicationContext() as context使用 getApplicationContext() 作为上下文时,在 android 6 中的 progressUpdate 上没有显示 Toast
【发布时间】:2016-07-14 18:02:58
【问题描述】:

我正在尝试在 ProgressUpdate() 方法上显示 Toast。在 Android 4,5 上运行良好,但在 android 6 上不显示 toast 并且没有抛出异常。

这是我的方法:

@Override
    protected void onProgressUpdate(String... values) { 
    super.onProgressUpdate(values);
    Toast.makeText(getApplicationContext(), values[0],     Toast.LENGTH_SHORT).show();
    }

如果我用 getBaseContext() 替换 getApplicationContext(),Toast 会按预期工作。

如果我使用 MainActivity.this 作为上下文,它不起作用

使用 getBaseContext() 是否正确? 为什么在 android 4,5 而不是 6 上使用 getApplicationContext()?

【问题讨论】:

    标签: android toast


    【解决方案1】:

    我不确定他们在进行 Android 6.0 更新时发生了什么,但他们肯定已经封装了一些上下文或类似的东西。以下是这两种方法之间的区别: Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

    Activity.getApplicationContext():返回整个上下文 应用程序(所有活动都在其中运行的进程)。 如果需要上下文,请使用 this 而不是当前的 Activity 上下文 与整个应用程序的生命周期相关联,而不仅仅是当前 活动。

    ContextWrapper.getBaseContext():如果你需要从 在另一个上下文中,您使用 ContextWrapper。所指的上下文 从内部通过 getBaseContext() 访问 ContextWrapper。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2020-03-29
      相关资源
      最近更新 更多