Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Holding context, activity or views as member of a class is bad performance?

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?

Imagine this activity:

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
   }
}

And what about adapters?

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;
   }
}

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?

Non-Activity class

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);
      }
   }
}

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

like image 628
Zbarcea Christian Avatar asked Feb 08 '14 10:02

Zbarcea Christian


People also ask

Should I use application context?

So always remember, in case of Singleton(lifecycle is attached to the application lifecycle), always use the Application Context. So, now when to use the Activity Context. Whenever you are in Activity, for any UI operations like showing toast, dialogs, and etc, use the Activity Context.

What is context leak Android?

Context-related memory leaks in Android Hence, all widgets in an Android app receive Context as a parameter in the constructor, holding a reference to the entire activity of the application. This also includes its View hierarchy and all other resources. Hence, if you leak the context then you will leak memory.

When would you call getApplicationContext?

This method is generally used for the application level and can be used to refer to all the activities. For example, if we want to access a variable throughout the android app, one has to use it via getApplicationContext().

What is the difference between application context and activity context?

They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.


1 Answers

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 returns the context of the single, global Application object of the current process so it can be used safely throughout your application code.

like image 85
makovkastar Avatar answered Oct 01 '22 10:10

makovkastar