ContentProvider执行时机

2022-09-30 08:04:54 浏览数 (1)

这里先给出源码分析的结论:

代码语言:javascript复制
Application的onAttachContext()先执行
Application 的 onCreate() 方法执行
ContentProvider 的 onCreate() 

源码:

代码语言:javascript复制
├── ActivityThread.handleBindApplication
    ├── LoadedApk.makeApplication
        ├── Instrumentation.newApplication
            ├── Instrumentation.newApplication
                ├── (Application)clazz.newInstance() //调用构造方法
                ├── Application.attach
                    ├── Application.attachBaseContext //调用attachBaseContext方法
    ├── ActivityThread.installContentProviders
        ├── ActivityThread.installProvider
            ├── ContentProvider.attachInfo
                ├── ContentProvider.attachInfo
                    ├── ContentProvider.onCreate  // 调用 ContentProvider的onCreate方法
    ├── Instrumentation.callApplicationOnCreate
        ├── Application.onCreate  //调用Application的onCreate方法

结论:

调用顺序: Application 构造方法 –> Application.attachBaseContext –> ContentProvider.onCreate –> Application.onCreate –> Activity.onCreate

0 人点赞