context

来源:互联网 发布:广东省大数据局级别 编辑:程序博客网 时间:2024/06/06 05:33

getApplicationContext()、getBaseContext()、this的区别

In various bits of Android code I've seen: 

 

public class MyActivity extends Activity { 
    public void method() { 
       mContext = this;    // since Activity extends Context 
       mContext = getApplicationContext(); 
       mContext = getBaseContext(); 
    } 
}

However I can't find any decent explanation of which is preferable, and under what circumstances which should be used.

I agree that documentation is sparse when it comes to Contexts in Android, but you can piece together a few facts from various sources.

 

This blog post on the official Google Android developers blog was written mostly to help address memory leaks, but provides some good information about contexts as well:

  1. 几个Context的区别
  • In a regular Android application, you usually have two kinds of Context, Activity and Application.
  • Basically the Application context is associated with the Applicaiton and will always be the same throughout the life cycle of you app, where as the Activity context is associated with the activity and could possible be destroyed many times as the activity is destroyed during screen orientation changes and such.
  • Don't use getBaseContext(), just use the Context you have.
  • So overall it seems preferable to use the global application context when possible.

 

然而getApplicationContext有时会引起问题

 

-------------------------------------------------

在使用context的时候,小心内存泄露,防止内存泄露,注意一下几个方面:

 1. 不要让生命周期长的对象引用activity context,即保证引用activity的对象要与activity本身生命周期是一样的

 2. 对于生命周期长的对象,可以使用application context

 3. 避免非静态的内部类,尽量使用静态类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化

 

然而老子这段话基本看不明白


对于第三点,常见的就是handler这个内部类,具体原因:

 内部类持有外部类Activity的引用,当Handler对象有Message在排队,则无法释放,进而导致Activity对象不能释放。

如果是声明为static,则该内部类不持有外部Acitivity的引用,则不会阻塞Activity对象的释放。

内部类知识:

  • 静态嵌套类的对象,并不需要其外围类的对象。 即它可以不依赖于外部类实例被实例化。
  • 内访外:不能从嵌套类的对象中访问非静态的外围类对象
  • 外访内:外部类访问内部类的的成员有些特别, 不能直接访问, 但可以通过内部类实例来访问
  • 静和非静区别:静态内部类可以有静态成员,而非静态内部类则不能有静态成员
  • 静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量(静态内部类的静态成员能否访问外部类的静态变量呢?我还没测试)
  • 非静态内部类的非静态成员可以访问外部类的非静态变量

为了避免handler有消息还在排列,那可以在activity的生命周期里这么搞:

public void onDestroy() {
    mHandler.removeMessages(MESSAGE_1);
    mHandler.removeMessages(MESSAGE_2);
    mHandler.removeMessages(MESSAGE_3);
    mHandler.removeMessages(MESSAGE_4);
 
    // ... ...
 
    mHandler.removeCallbacks(mRunnable);
 
    // ... ...

 

}
 
 
----------------------------------------------------------
静态变量和activity
 
public class ErrorTestActivity extends Activity {
    /** Called when the activity is first created. */
    private static boolean Flag = false;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i("wangchao","wocao"+Flag);
        Flag = true;
        
    }
}
问题:
  • 程序第一次运行,打印信息为false,之后推出程序,再次运行,打印信息为true。
  • 当程序退出的时候,定义的Flag不会被销毁么?为什么还会保留下来?
  • 什么时候才能销毁static变量呢?
答案:
当窗口消毁时,Activity的静态变量是存在的,因为静态变量是属全局变量、静态变量是整个应用程序的公共变量(即使你这个地方写的是私有),所以Activity消毁时,静态变量是不会清除的。但当什么时候才会清除呢,上面说过静态变量是整个应用程序的,所以只有当各个应用程序的进程消毁时,静态变量才会销毁


 一句话,不要有那种你的activity被销毁了但还在执行的东西,比如线程,比如一些静态变量
0 0
原创粉丝点击