android - is it bad to use public static fields/variables? -


Started learning about Android development, and I could read about the stable variable and be screwed because the garbage is not collectable.

I have used something in some circumstances, but I am very worried that it can leak memory.

Can anyone see me on my code below and see if they leak memory or not?

MainActivity.java

  the public class increases the main activity activity {public static Boolean IS_ACTIVITY_OPEN; Public static image view image; Override public null on @reate (bundle saved instainstate) {super.naught (savedinstenstate); SetContentView (R.layout.main_layout); IS_ACTIVITY_OPEN = True; .... .... VoiceReserve = New Broadcast Receiver () {@ Override Public Wide On Receive (Final Reference Reference, Intent of Intent) {.....}; Register receiver (voice receiver, new intent filter (broadcast receiver. Voice intent); @ Override Public Watch OnDestoy () {Super. One day (); UnregisterReceiver (VoiceReceiver); IS_ACTIVITY_OPEN = False; }}  

image.java

  main activity. Image.setImageBitmap (resizedBitmap);  

.....

.....

Broadcast. Java

  if ( ! MainAcitivty.IS_ACTIVITY_OPEN) {// starting an activity Intent intent2 = new intent (reference, MainAcitivty.class); Intent2.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); Context.startActivity (intent2); Handler = new handler (); MyPostDelay = New Runnabal () {@Override Public Zero Run () {context.sendBroadcast (New Intent)); }}; Handler.post Delighted (Myopost Dale, 300); } And {reference. SendBroadcast (new intent (voucher)); }  

Thank you very much in advance.

See the article on the Android Developers blog, or reference to any other category (like any In view of the context, a stable field containing a (strong) reference will mean that the garbage collector will not be able to retrieve the allocated storage. If context is an application by context then it is okay because they Live for your app and did not collect garbage anyway Will go But in the case of scenes, the context is possibly an activity that should be collected as soon as possible.

It does not mean that all the stable fields will leak extinction memory if they are more complex classes with primitive types, or weak contexts of ordinary classes or other classes. , They can not stop the garbage collector from recovering too much memory. But generally the stable and especially public stable fields have a code odor and it should probably be saved later, so that the code is easy to maintain later.


Comments