将上下文,活动或视图作为类的成员来表现不好? [英] Holding context, activity or views as member of a class is bad performance?

查看:79
本文介绍了将上下文,活动或视图作为类的成员来表现不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某处有一个红色,将视图保留为活动的成员会降低性能,因为每个视图都保留对其父Context的引用,它将填充堆.这是真的吗?

I have red somewhere that keeping views as members of an activity is bad performance, because each views is keeping a reference to its parent Context and it will fill up the heap. Is this true?

想象一下这个活动:

public class MyActivity extends FragmentActivity{
   private RelativeLayout mainLayout;
   private LineraLayout menuLayout;
   private FrameLayout tableLayout;

   private Button buttonOk;
   private Button buttonCancel;

   @Override
   protected void onCreate(Bundle bundle){
      super.onCreate(bundle);

      mainLayout = (RelativeLayout) findViewById(R.id.mainlayout);
      // And inflating other views
   }
}

适配器呢?

public class MyAdapter extends BaseAdapter{

   private MyActivity activity;
   private ArrayList<MyObjects> myObjects;   

   public MyAdapter (MyActivity activity, ArrayList<MyObjects> myObjects){
      this.activity = activity;
      this.myObjects = myObjects;
   }
}

这是不好的表现吗?将活动作为参数而不是上下文传递是否不好?如果我想从适配器的父MyActivity类访问公共方法怎么办?

Is this bad performance? Is it bad to pass an activity as a parameter instead of a Context? What if I want to access public methods from the parent MyActivity class from the adapter?

非活动类

public MyDatabase{
   private Context context;
   private SQLiteDatabase db;

   public MyDatabase(Context context){
      this.context = context;
      this.db = new DatabaseHelper(context).getWritableDatabase();
   }

   public Object getData(int id){
      return db.query(params...);
   }

   public static class DatabseHelper extends SQLiteOpenHelper{
      public DatabaseHelper(Context context){
         super(context, "my_db", null, 1);
      }
   }
}

为什么人们说当类构造函数期望Context作为参数时,您应该传递getApplicationContext()而不是and Activity?

Why people are saying that when a class constructor expects a Context as a parameter, you should pass getApplicationContext() instead of and Activity?

推荐答案

Activity实例传递给某个方法或将其引用存储在某个地方是一种不好的做法,因为在配置更改期间Android会创建一个Activity的新实例并旧的应该由垃圾收集器清除.但是,如果有人持有对旧Activity对象的引用,则在引用存在之前,GC不会收集该对象.这样就会发生内存泄漏.

To pass Activity instance to some method or store a reference to it somewhere is a bad practice because during configuration change Android creates a new instance of an activity and old one should be removed by garbage collector. But if someone holds a reference to an old Activity object it will not be collected by GC till reference to it exists. So memory leak occurs.

但是在适配器构造函数的情况下,完全可以通过活动实例,因为适配器生命周期与活动生命周期相关.通常,活动后会被垃圾收集.

But in case of adapter constructor it's fully OK to pass activity instance because adapter lifecycle is coupled to activity lifecycle. Normally it will be garbage collected after activity.

getApplicationContext返回当前进程的单个全局Application对象的上下文,因此可以在整个应用程序代码中安全地使用它.

getApplicationContext returns the context of the single, global Application object of the current process so it can be used safely throughout your application code.

这篇关于将上下文,活动或视图作为类的成员来表现不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆