首页 技术 正文
技术 2022年11月12日
0 收藏 673 点赞 2,683 浏览 3147 个字
  • onAttachedToWindow在整个Activity的生命周期中占据什么位置?
  • 为什么要在onAttachedToWindow中修改窗口尺寸?

一、onAttachedToWindow在Activity生命周期中的位置

根据之前分析API的套路,看onAttachedToWindow在Android文档中怎样介绍。

首先看Window.Callback中关于onAttachedToWindow的介绍。

public abstract void onAttachedToWindow ()

Called when the window has been attached to the window manager. See View.onAttachedToWindow() for more information.

好吧,官方把我引导到了View中,那么恭敬不如从命,看View中怎么说。

protected void onAttachedToWindow ()

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called beforeonDraw(android.graphics.Canvas), however it may be called any time before the first onDraw — including before or after onMeasure(int, int).

从API说明我们可以定 位当View附加到窗体时,也就是View和Window绑定时就会调用这个函数,此时将会有一个Surface进行绘图之类的逻辑。并且发现 Window.CallBack是一个接口类,而且官方引导到了View中,那么可以大胆判断View实现了Window.CallBack的回调方法, 那么View和Window之间的关系便可以有个初步猜测。下篇博客再具体讨论DecorView和Window之间的关系。

接下来通过实验判断onAttachedToWindow在Activity整个生命周期中的位置。

实验很简单,将Activity各个生命周期打上log,然后看LogCat中的结果

onAttachedToWindow()在整个Activity生命周期的位置及使用

看来我们最终要找的生命周期为onCreate->onStart->onResume->onAttachedToWindow

然后通过Google找到了一张比较详细的Activity生命周期图,也印证了我们的实验结论。

onAttachedToWindow()在整个Activity生命周期的位置及使用

二、为什么要在onAttachedToWindow中修改窗口尺寸

为什么网上很多教程一定要在onAttachedToWindow()里修改高宽而不在onCreate中?这个问题没人解答,那么我就将代码

    View view = getWindow().getDecorView();
WindowManager.LayoutParams lp = (WindowManager.LayoutParams)view.getLayoutParams();
lp.gravity = Gravity.CENTER;
lp.width = (dm.widthPixels * 4) / 5;
lp.height = (dm.widthPixels * 4) / 5;
getWindowManager().updateViewLayout(view,lp);

放到onCreate中 进行测试,结果在lp.gravity = Gravity.CENTER;这行报了空指针异常,所以view.getLayoutParams()获取的LayoutParams是空,那么问题来 了!为什么onCreate()中DecorView的LayoutParams是空而onAttachedToWindow()中就不为空?要高清这个 问题就要看DecorView在什么时候设置的LayoutParam。

从博客Android应用窗口的视图对象的创建过程分析中发现源码

    public final class ActivityThread {
...... final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
...... ActivityClientRecord r = performResumeActivity(token, clearHide); if (r != null) {
final Activity a = r.activity;
...... // If the window hasn't yet been added to the window manager,
// and this guy didn't finish itself or start another activity,
// then go ahead and add the window.
boolean willBeVisible = !a.mStartedActivity;
if (!willBeVisible) {
try {
willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
a.getActivityToken());
} catch (RemoteException e) {
}
}
if (r.window == null && !a.mFinished && willBeVisible) {
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
......
if (a.mVisibleFromClient) {
a.mWindowAdded = true;
wm.addView(decor, l);
}
} ......
} ......
} ......
}

原来在 ActivityThread执行handleResumeActivity时就会为 PhoneWindow(r.activity.getWindow)中的DecorView设置LayoutParam,并且通过源码发现 handleResumeActivity函数首先会执行performResumeActivity,此时会调用Activity的 onResume()生命周期函数,这时问题就比较清晰了,看来只要在Activity的onResume生命周期后就能获取DecorView的 LayoutParam,进而可以设置高度和宽度了。根据上面贴出的生命周期图,onResume()后面是onAttachedToWindow(), 并且onAttachedToWindow只会调用一次,不会受用户操作行为影响。所以在onAttachedToWindow中进行窗口尺寸的修改再合 适不过了。

总结:

    • onAttachedToWindow运行在onResume之后;
    • DecorView的LayoutParams是在ActivityThread的handleResumeActivity中设置的,并且该函数会调用Activity的onResume生命周期,所以在onResume之后可以设置窗体尺寸;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300