Skip to content Skip to sidebar Skip to footer

Is It A Good Idea To Declare Static View In Android?

The problem : I have one class and one activity: The class do the logic and use a complex algorithms that manipulate Textview and ImageView from the activity What I'm doing : In a

Solution 1:

Declaring static views is a bad practice especially in your scenario wherein you declare it inside an activity. If your application grows, you might encounter memory problem exception that will result to application crashing because static views that you declare cannot be collected by the garbage collector in order to free up memory heap.


Solution 2:

I can't see any reason you'd ever want to do that. Static variables are shared between all instances of a class. But views are very much bound to their particular instance of Activity, they cannot be used by multiple instances- each instance must create their own. A View created by one instance of an Activity cannot be displayed in a second instance. So I can't see any case where a static View is a benefit.


Solution 3:

Declaring views as static is not suggested for few reasons :

  1. It would lead to memory leak
  2. Static view referenced in one instance won't be available to the other instance
  3. View should only be referenced when the activity is created. Having a static view means we can assign it a reference even before activity is created which leads to problem.

In your case u can return the manipulated text or the new image and display the text or the image from the activity.


Post a Comment for "Is It A Good Idea To Declare Static View In Android?"