When I create a button:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = …;
Button btn = new Button(this);
btn.setText("My Button");
layout.addView(btn);
}
If I don't keep a strong reference to the btn
, does Android keep the btn
instance alive?
For example, does layout.getChildView(0)
return the exact instance ( btn
)? or does Android create and return a new instance of Button
and return it?
I'm not talking about subclassing, (eg class MyButton extends Button
) which I think it's obvious it must be kept in memory, I am only asking about built-in view classes.
ViewGroup stores strong references on its children in an array. If you take a look into ViewGroup sources you find following field:
private View[] mChildren;
And when you call getChildAt
you get an instance from this array:
public View getChildAt(int index) {
if (index < 0 || index >= mChildrenCount) {
return null;
}
return mChildren[index];
}
对于任何视图组,当您一次添加任何子视图时,它不会创建新实例。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.