Frank Cheng
Frank Cheng

Reputation: 6196

when to call view's GetParent method, how is view's life-cycle

when i call view's GetParent method in Constructor, it returns null. So where should i call this method to get the view`s parent. thanks

Upvotes: 0

Views: 2745

Answers (1)

MrJre
MrJre

Reputation: 7161

It returns null because in the constructor the view doesn't have a parent yet. It gets the parent when you actually add the view to another view by doing addView(yourView); short example:

LinearLayout layout = new LinearLayout(context);
YourView yourView = new YourView(context); // yourView constructor gets called
layout.addView(yourView); // parent gets set to layout

Upvotes: 1

Related Questions