【问题标题】:What is the difference between context and activity inside a fragment? [duplicate]片段内的上下文和活动有什么区别? [复制]
【发布时间】:2019-12-25 11:06:22
【问题描述】:

当我在片段中时,this.contextthis.activity 有什么区别? 例如:

val intent = Intent(this.activity!! , Activity::class.java)

val intent = Intent(this.context!! , Activity::class.java)

【问题讨论】:

标签: java android kotlin


【解决方案1】:

with context > 只要应用程序存在,意图就存在

有活动>只要活动存在,意图就存在

现在您知道大多数情况下您需要什么了.. :-D

【讨论】:

  • 是的,这个答案很好
  • @HoàngVũAnh 别忘了给兄弟点个赞... :-)
  • 是的,我会做的
  • 我认为片段中的getContext 不会返回应用程序。见这里:stackoverflow.com/questions/32227146/…
【解决方案2】:

Context 是 Activity 的超类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2021-09-03
    • 2012-07-19
    • 1970-01-01
    • 2015-11-23
    相关资源
    最近更新 更多