关闭

Android读源码--application篇

251人阅读 评论(0) 收藏 举报
分类:

感觉android好水好水,然后又突然迷失了方向,感觉最近工作中总是在做一些重复的事情,前些天做了下ListView嵌套,就类似QQ空间评论那种,这周又做了个类美团的双ListView联动菜单,本来该总结一下,可是写出来之后又懒得总结了,东西太多,然后闲着没事,看下源码吧。

原谅我的才疏学浅,估摸自己现在的身份也就是个开发小菜鸡,鸟都算不上 ~~衰~~~  看代码都不知道从何处下手,本来想着接触最多的应该是activity,可是看着发现好多,于是决定先看application吧,毕竟app初始化最先调用的东东,看着看着才发现应该从context入手的,不说了,application记录如下,肯定有不对的地方,但仅仅是自己对application的理解,以目前的水平来说,所能解释和概括的也就这些了。



application类 第一个私有变量

#1.    private ArrayList<ComponentCallbacks> mComponentCallbacks =
            new ArrayList<ComponentCallbacks>();

而点开CommentCallbacks之后就是

void onConfigurationChanged(Configuration newConfig);
void onLowMemory();

两个抽象方法。

void onConfigurationChanged(Configuration newConfig);
接触到的 横竖屏切换时设置 android:configChanges="orientation"时切竖屏调用
或者ndroid:configChanges="orientation|keyboardHidden" 横竖屏执行此函数


附:activity横竖屏切换时生命周期总结
1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次


2、设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次


3、设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法


void onLowMemory();
官方注释:This is called when the overall system is running low on memory, and
      actively running processes should trim their memory usage.  While
      the exact point at which this will be called is not defined, generally
      it will happen when all background process have been killed.
      That is, before reaching the point of killing processes hosting
     service and foreground UI that we would like to avoid killing.
看名字猜到应该是在低内存时调用函数。看注释也是当低内存时会调用此函数,
并且给出尽量保持前台进程(直面用户),以防止运行内存被释放的建议。
You should implement this method to release any caches or 
other unnecessary resources you may be holding on to.The 
system will perform a garbage collection for you after returning from this method. 

Preferably, you should implement ComponentCallbacks2.
onTrimMemory from ComponentCallbacks2 to incrementally unload your resources 
based on various levels of memory demands. That API is available for API level 14 and higher, 
so you should only use this onLowMemory method as a fallback for older versions, 
which can be treated the same as ComponentCallbacks2.onTrimMemory with the ComponentCallbacks2.TRIM_MEMORY_COMPLETE level.

在低内存时实现接口这个方法去释放不必要的资源,系统会提供给你一个垃圾回收器
在 api leavel 14及以上可以实现 ComponentCallbacks2 并实现
void onTrimMemory(int level);方法去管理内存。


#2.  private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
            new ArrayList<ActivityLifecycleCallbacks>();


ActivityLifecycleCallbacks
Activity生命周期回调函数...

public interface ActivityLifecycleCallbacks {
        void onActivityCreated(Activity activity, Bundle savedInstanceState);
        void onActivityStarted(Activity activity);
        void onActivityResumed(Activity activity);
        void onActivityPaused(Activity activity);
        void onActivityStopped(Activity activity);
        void onActivitySaveInstanceState(Activity activity, Bundle outState);
        void onActivityDestroyed(Activity activity);
    }

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:60142次
    • 积分:1204
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:11篇
    • 译文:1篇
    • 评论:30条
    最新评论