关于android activity加载完毕事件

很多人都想在activity加载完毕的同时处理一下事件,如弹出一个popWindow或者其他,但在onCreate方法中处理这个时间的话,程序会报错:大意是activity还没有加载完,不能处理弹窗事件,其实不光是弹窗,只要用到activity.this的事件,在activity加载完之前都不能处理,原因很明显——popWindow或者是dialog的显示都要依附在一个parent Activity上,所以在Activity加载完之间,他们都不能显示。

不过万幸的是android给开发者提供了一个方法,可以解决这个问题,这个方法就是onWindowFocusChanged()

其说明如下:

Called when the currentWindowof the activity gains or loses focus.This is the best indicator of whether this activity is visible to the user. The default implementation clears the key tracking state, so should always be called.

Note that this provides information about global focus state, which is managed independently of activity lifecycles. As such, while focus changes will generally have some relation to lifecycle changes (an activity that is stopped will not generally get window focus), you should not rely on any particular order between the callbacks here and those in the other lifecycle methods such asonResume().

As a general rule, however, a resumed activity will have window focus... unless it has displayed other dialogs or popups that take input focus, in which case the activity itself will not have focus when the other windows have it. Likewise, the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take window input focus without pausing the foreground activity.

Java代码 收藏代码
  1. @Override
  2. publicvoidonWindowFocusChanged(booleanhasFocus)
  3. {
  4. if(hasFocus)
  5. {
  6. }
  7. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值