为什么ReferenceQueue在活动中始终为空?

时间:2017-11-14 05:50:53

标签: java android weak-references

我正在尝试使用ReferenceQueue来检查活动是否存在活动,此活动是否被回收 我只是将意图用于Main2Activity.class:

     switch (v.getId()){
        case R.id.btn_turn_two:
            Intent intent=new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
            break;
            default:
                break;
    }

当我按下返回MainActivity.class时,Main2Acitivity.class将被销毁,所以我在应用程序中使用registerActivityLifecycleCallbacks监视活动,

public class MainApplication extends Application{
private watche watche;
@Override
public void onCreate() {
    super.onCreate();
    watche=new watche(this,getApplicationContext());
}

此类检查Activity是否已被破坏。

public class watche {
KeyRefrence ref;
private Context context;


private final Set<String> retainedKeys = new CopyOnWriteArraySet<>();
private final ReferenceQueue<Object> referenceQueue=new ReferenceQueue<>();

public  watche(Application application, Context context){
    application.registerActivityLifecycleCallbacks(lifecycleCallbacks);
    this.context=context;
}
Application.ActivityLifecycleCallbacks lifecycleCallbacks=new Application.ActivityLifecycleCallbacks() {
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    }
    @Override
    public void onActivityStarted(Activity activity) {
    }
    @Override
    public void onActivityResumed(Activity activity) {
    }
    @Override
    public void onActivityPaused(Activity activity) {
    }
    @Override
    public void onActivityStopped(Activity activity) {
    }
    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }
    @Override
    public void onActivityDestroyed(Activity activity) {
        //weakrefrence to this activity
        String key = UUID.randomUUID().toString();
        retainedKeys.add(key);
       final KeyRefrence  weakReference=new KeyRefrence(activity,key,activity.getPackageName(),referenceQueue);
       final MyAyTask myAyTask=new MyAyTask(weakReference,activity,context);
        myAyTask.doInBackground(null);

     }
};

public class MyAyTask extends AsyncTask {
    private Activity activity;
    private KeyRefrence keyRefrence;
    private Context context;
    public MyAyTask(KeyRefrence keyRefrence,  Activity activity,Context context){
        this.activity=activity;
        this.context=context;
        this.keyRefrence=keyRefrence;
    }
    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
    }
    @Override
    protected Object doInBackground(Object[] objects) {
        gc();
        ref=(KeyRefrence)referenceQueue.poll();
        if (ref==null){
            Log.d(TAG, "onActivityDestroyed:Acitivty is not destory");
        }
        else if (ref!=null){
            Log.d(TAG, "doInBackground: "+ref);
        }
        return null;
    }
}

我已经创建了KeyReference实现WeakReference,而我在Main2Activity.class中没有做任何事情,我只是按下手机返回,我检查了gc,是否正常工作,但是引用队列总是空的,我确定Activity是destoryed。

0 个答案:

没有答案