Monday, 15 March 2010

Static Utility class with Context/Activity - Android -


During the development of the Android app, I have come to collect utility-type methods which I have put in a static.

However, now I have some methods that require references to the activity and that which is required is the activity itself. Let me give examples of some of them:

  • getDeviceNaturalOrientation () - Uses getWindow () of an activity. GetWindowManager (). GetDefaultDisplay () orientation to display the rotation, width and height to determine the display's natural.
  • getDeviceOrientation () - To get the current orientation but to create the current orientation
  • createFile () - Uses the reference to reach and create and show some resources (wire) SOME LOST

    Now, I have big questions about this bogus class:

    1. So far, every function takes a reference parameter, whatever I I am on activity, but is there a stable context or activity variable in the UTILS class Nanny and have to set it at the beginning of every activity (like on crick)? This variable will be used in the context of any context or activity.
    2. Assume above that it is not recommended, whether it is ok to pass an activity parameter in a method or just as an activity reference? The methods I use for the above device orientation tasks are not specific to the activity objects, not the reference, so I pass pass as an activity or pass in the form of the event and put it into action (after Looks like a terrible idea in). Apart from this, I am very open to the idea that the Util Class is not the way to go for these methods, which requires reference / activity, so I welcome those options. I'll still be able to stop copies of these methods in every activity that class uses them.

      1) A constant link for reference can cause memory leakage. This means that even a reference to the activity will be placed in the static variable even after being destroyed, so all memories of the activity and all its thoughts will be valid and will not be cleared by GC. You can do this, but whenever you are done, be careful to take out the variable. Its better to avoid it properly.

      2) The activity is slightly weird for passing the activity, but there is no technical reason for this. At that time we are debating the code cleaning / maintenance. And there are times when non-clean solutions are very easy. Of course, in the above cases, I pass the function rather than passing the orientation / display / resource items in full context or making special accessories.

      No comments:

      Post a Comment