ContextWrapper中attachBaseContext()方法使用技巧

2018-02-17 18:53:18

ContextWrapper中attachBaseContext()方法使用技巧。

ContextWrapper类的源码,ContextWrapper中有一个attachBaseContext()方法,这个方法会将传入的一个Context参数赋值给mBase对象,之后mBase对象就有值了。
Application中在onCreate()方法里去初始化各种全局的变量数据是一种比较推荐的做法,但是如果你想把初始化的时间点提前到极致,也可以去重写attachBaseContext()方法,如下所示:
这里写图片描述

?

1
2
3
4
5
6
7
8
9
10
public class MyApplication extends Application { 
 
    @Override 
    protected void attachBaseContext(Context base) { 
        // 在这里调用Context的方法会崩溃 
        super.attachBaseContext(base); 
        // 在这里可以正常调用Context的方法 
    
 
}


  • 2017-02-24 15:53:02

    PHP中__get()和__set()的用法实例详解

    在PHP5中,预定义了两个函数“__get()”和“__set()”来获取和赋值其属性,对每个字段进行set和get的操作。只需要加上两个魔术方法即可

  • 2017-02-24 16:53:58

    PHP 中的Closure

    Closure,匿名函数,又称为Anonymous functions,是php5.3的时候引入的。匿名函数就是没有定义名字的函数。这点牢牢记住就能理解匿名函数的定义了。

  • 2017-03-02 09:45:27

    动态加载js和css

    开发过程中经常需要动态加载js和css,今天特意总结了一下常用的方法。