java - What's wrong with using static for Parent Activities? -


let's have parent activity parenthost static reference:

   parenthost extends fragmentactivity {    static parenthost parent_host;    static viewpager pager; 

now let's use reference like:

public class testpage extends fragment {                button.setonclicklistener(new  view.onclicklistener() {                 @override                 public void onclick(view v) {                     //what's wrong this?                     parenthost.pager.setcurrentitem(1);                     or:                     parenthost.parent_host.finish();                 }             }); 

i've tried breaking app (start,pause,killed ram) , haven't seen issues.

is method ok parent activities?

for future readers:

switching ((parenthost) getactivity()).whatever(), appropriate way of using instance of running class. gabe's comment:

the reason safe held in instance variable , not in static. though there's 1 reference gc can tell reference held 1 of children , whole chain can gced (the ability why java uses mark , sweep style gc instead of reference count)

well don't need it. fragment can activity calling getactivity().

in addition, memory leak. if fragment has static reference activity, activity never cleaned. means object holds never cleaned. means of views, bitmaps, , memory structures, kill ram.

so pretty bad idea, , gives no benefit. why want it?


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -