Android 的onApplyThemeResource onWindowFocusChanged onAttachedToWindow

来源:互联网 发布:黑客攻防宝典知乎 编辑:程序博客网 时间:2024/04/27 15:56

之前看Googl提供的Android API 没有发现有onWindowFocusChanged(boolean hasFocus)这个方法,我还以为Activity的生命周期就只有那表述的7个方法而已

无意中测试的项目的时候发现了这个方法,我就随意的测试了一下,Activity 启动的时候最先调用的是这个onApplyThemeResource(Theme theme, int resid, boolean first)

方法,然后依次是:onCreate(Bundle savedInstanceState)---->onContentChanged() --->onStart() ---->onResume()----->onPostResume()---->onAttachedToWindow() ---->onWindowFocusChanged(boolean hasFocus)------>onPause()---->onWindowFocusChanged(boolean hasFocus)--->onStop() ---->onDestroy()

到这里我发现Android本身就是一个大框架,这些方法不管你使用与否,Android本身会自动去执行,想要测试这些方法也很简单,可以在这些方法里面写一些代码,然后看Activity的效果。

Activity生命周期中,onStart, onResume, onCreate都不是真正visible的时间点,真正的visible时间点是onWindowFocusChanged()函数被执行时。
从onWindowFocusChanged被执行起,用户可以与应用进行交互了,而这之前,对用户的操作需要做一点限制。一下这些都可以在这个方法内获取

this.getActionBar();
this.getApplicationContext();
this.getBaseContext();
this.getApplicationInfo();
this.getCallingPackage();
this.getChangingConfigurations();
this.getClassLoader();
this.getExternalCacheDir();
this.btnLogin.getWidth();

Android框架有很多有趣 的操作,细细的琢磨肯定能发现很多亮点。。。



原创粉丝点击