I have made a view that is named as menu. I have made it a singleton. this is it’s constructor:
private Menu(Activity cx) {
super(cx);
this.context = cx;
// something = AsyncTaskGetAllStreams.numberOfStreams;
LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
li.inflate(R.layout.menu, this, true);
setActivity(cx);
initUI();
initUI(MyStreamsActivity.streamsJoinedByUser2.length);
assignVal();
}
The problem i am having is that once this layout’s activity is set it shows on that activity and on any other it tell me this in the LogCat:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
For your reference its stack trace starts from my button’s onClick(). this is the code that I process in the onClick()
Intent intent = new Intent(activity, MyPostActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(intent);
Why is this happening? what should be done?
I think using a singleton design pattern here is not correct. This is also what is causing your problem. A view can only have a single parent, and in your example above you are inflating your view into a parent – so you cannot do this again until you remove it from the current parent.
Your singleton use in this case is breaking the flexibility of Android’s framework. Don’t fight the SDK, let it work for you.