如何使用onWindowFocusChanged()方法? [英] How to use onWindowFocusChanged() method?

查看:673
本文介绍了如何使用onWindowFocusChanged()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对活动更改时如何触发事件感到困惑.我知道的一件事是使用onWindowFocusChanged()方法,但我不知道如何使用它.请举一个例子,例如,当活动发生变化时,它应该显示一个吐司,说活动已更改".

I am confused in how to trigger an event when the activity is changed. One thing that I know is using onWindowFocusChanged() method but I don't know how to use it. Please help in one example i.e. when the activity changes, it should show a toast said "Activity changed".

推荐答案

当窗口的焦点改变时,将调用以下方法.例如,如果窗口获得/丢失焦点,则将触发它.如果窗口获得焦点,则hasFocus为true,否则为false.

The following method gets called when window's focus changes. For instance, if the window gains/losses focus, it will be triggered. In case, the window gains focus, hasFocus is true and false otherwise.

 public void onWindowFocusChanged(boolean hasFocus) {         
     super.onWindowFocusChanged(hasFocus);
     if(hasFocus) 
         Toast.makeText(context, text, duration).show();
 }

这篇关于如何使用onWindowFocusChanged()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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