是否总是安全投上下文到活动中查看 [英] Is it always safe to cast Context to Activity within View

查看:115
本文介绍了是否总是安全投上下文到活动中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

五月我知道,它是安全的,我总是投上下文活动 A <$ C $内C>查看?

May I know, is it safe for me to always cast Context to Activity within a View?

View {
    Activity activity = (Activity)this.getContext();
}

到目前为止,它工作正常所有的时刻。我想知道,是否有任何优势的情况下,上述code会失败?

So far, it works fine all the moment. I was wondering, is there any edge cases that the above code will fail?

推荐答案

虽然我不认为这样的情况下,我想是不是有两个原因这样一个伟大的想法:

While I can't think of such case, I think it is not such a great idea for two reasons:

  1. 为什么你要做到这一点,当你明确需要的活动?
  2. 如果明天这将改变,还会有其他的上下文查看
  1. Why would you want to do that, when do you explicitly need Activity?
  2. What if tomorrow this will be changed, and there will be other context for View?

这篇关于是否总是安全投上下文到活动中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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