获取Context对象
2018-11-01 本文已影响0人
奈蜇
通常方法:
private static Context mContext;
public static Context getContext() {
return mContext;
}
@Override
public void onCreate() {
super.onCreate();
mContext = this.getApplicationContext();
}
我们会直接在Application中写一个static Context然后再onCreate赋值,由于Application在程序运行中只onCreate一次且伴随程序的生命周期,所以没很大问题的。
但是如果我们要随时的使用伴随Activity生命周期的Context怎么办呢。
这里我们采用WeakReference来一直持有当前显示页的Context。
在BaseActivity中去更新Application 中 保存Context的WeakReference,使其一直保存着显示页的Context。
话不多说直接上代码:
BaseApplication:
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import java.lang.ref.WeakReference;
/**
* Created by 奈蜇 on 2018-11-01.
* BaseApplication
*/
public abstract class BaseApplication extends Application {
private String TAG;
@SuppressLint("StaticFieldLeak")
private static Context mContext;
private volatile static WeakReference<Context> mContextWeakReference = null;
public static Context getContext() {
if (mContextWeakReference == null || mContextWeakReference.get() == null) {
synchronized (BaseApplication.class){
if(mContextWeakReference == null || mContextWeakReference.get() == null){
mContextWeakReference = new WeakReference<>(mContext);
}
}
}
return mContextWeakReference.get();
}
public static void setContextRef(Context contextRef){
mContextWeakReference = new WeakReference<>(contextRef);
}
@Override
public void onCreate() {
TAG = this.getClass().getName();
LOG.D(TAG,"onCreate:" + this.toString());
super.onCreate();
mContext = this.getApplicationContext();
}
/**
* 在开程序在内核中结束的时候执行,退出程序不一定会执行.
*/
@Override
public void onTerminate() {
LOG.D(TAG,"onTerminate:" + this.toString());
TAG = null;
super.onTerminate();
}
/**
* 配置改变时触发这个方法,例如屏幕方向切换.
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
/**
* 内存匮乏时会调用.
*/
@Override
public void onLowMemory() {
super.onLowMemory();
}
/**
* 每次计算进程优先级满足条件时会调用,回调不同的内存状态.
*/
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
}
}
BaseActivity:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
/**
* Created by 奈蜇 on 2018-11-01.
* 基础活动类
*/
public abstract class BaseActivity extends AppCompatActivity {
private String TAG;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
TAG = this.getClass().getName();
LOG.D(TAG, "onCreate: " + this.toString());
super.onCreate(savedInstanceState);
BaseApplication.setContextRef(this);
}
@Override
protected void onStart() {
LOG.D(TAG, "onStart: " + this.toString());
super.onStart();
}
@Override
protected void onResume() {
LOG.D(TAG, "onResume: " + this.toString());
super.onResume();
BaseApplication.setContextRef(this);//保证弱引用一直引用当前的可见页面
}
@Override
protected void onPause() {
LOG.D(TAG, "onPause: " + this.toString());
super.onPause();
}
@Override
protected void onStop() {
LOG.D(TAG, "onStop: " + this.toString());
super.onStop();
}
@Override
protected void onRestart() {
LOG.D(TAG, "onRestart: " + this.toString());
super.onRestart();
}
@Override
protected void onDestroy() {
LOG.D(TAG, "onDestroy: " + this.toString());
TAG = null;
super.onDestroy();
}
}