-1

In my android application I just want development tips,

If i want to just set value/image only one time at onCreate().
1 way) Which is best way, by initialize object.
Or
2 way) using anonymous object.

((TextView) findViewById(R.id.txtView_footer_left)).setText("Text added at run time");

Above case with different condition, now i want to access textview/imageview 2 or 3 times then which way is better 1st way or 2nd way.

Static: in which cash we should use static keyword?

final static: in which cash we should use final static keyword?

My main agenda is.

i want to write code which is more robust, faster and less memory consumption.

let me also help in GC behavior.

Bhavesh Jethani
  • 3,891
  • 4
  • 24
  • 42
  • @lenabru Thanks for your answer and please give more tips for development so i can develop powerful app with good performance. – Bhavesh Jethani Jul 15 '14 at 06:15
  • i will gladly help you, however the tips you request, are specific per app requirements ;), so if you have any questions feel free to ask me – Lena Bru Jul 15 '14 at 06:23

2 Answers2

3

findViewById is a recursive costly operation. You want to limit these calls to the minimum. So if you want to use the object more than once, keep a reference to it

Lena Bru
  • 13,521
  • 11
  • 61
  • 126
  • 1
    Also, see the ViewHolder pattern: http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder – Blacklight Jul 15 '14 at 06:14
  • **Avoid Creating Unnecessary Objects** Object creation is never free. A generational garbage collector with per-thread allocation pools for temporary objects can make allocation cheaper, but allocating memory is always more expensive than not allocating memory. https://developer.android.com/training/articles/perf-tips.html#ObjectCreation – Bhavesh Jethani Jul 15 '14 at 06:50
  • please explain me i really very much confuse. – Bhavesh Jethani Jul 15 '14 at 06:51
  • @BhaveshJethani What else is there to explain? We won't do it better by just rephrasing the documentation. This is beyond the scope of comments on SOF. – Blacklight Jul 15 '14 at 07:01
  • @Blacklight As per my understanding, Android document is talking us to avoid unnecessary obj. if i want to use 2 3 times should i have to create reference of the object? – Bhavesh Jethani Jul 15 '14 at 08:48
0

i want to write code which is more robust, faster and less memory consumption.

If you are accessing more that once, initilize by static final

final Textview mTextView = (TextView)findViewById(R.id.text_view);

Performance Tips

VenomVendor
  • 15,064
  • 13
  • 65
  • 96
  • 1
    it is never good to keep **static** references to views. They prevent activity from being released when he's done with it – Lena Bru Jul 15 '14 at 06:22
  • I don't think that's going to work for two reasons: 1) `findViewById` isn't `static` and 2) your view hasn't been inflated at that point so it'll return `null`. – Jan Van den bosch Jul 15 '14 at 06:24
  • @JanVandenbosch Nope, It doesn't return null you can try it. – VenomVendor Jul 15 '14 at 06:29
  • 1
    @LenaBru oh, I did not know that, I have been using this way as suggested in [android Performance Tips](https://developer.android.com/training/articles/perf-tips.html#UseFinal) Thanks for the info, but I will not accept it, unless you can show me a proof. – VenomVendor Jul 15 '14 at 06:32
  • 1
    @VenomVendor You cannot compare primitive data types in static final constants to views (with a lifecycle)! You are interpreting the documentation incorrectly. – Blacklight Jul 15 '14 at 06:36
  • 1
    @VenomVendor: it doesn't even compile because of the `static` keyword and if I remove it, `findViewById` throws a NullPointerException. It doesn't work. – Jan Van den bosch Jul 15 '14 at 06:47
  • @JanVandenbosch Oops, yeah, I have used only `final` & not `static` to views. Just checked my code & made an edit. – VenomVendor Jul 15 '14 at 07:21