La vista no se vuelve a mostrar en otras actividades.

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) {
    this.context = cx;
    // something = AsyncTaskGetAllStreams.numberOfStreams;
    LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    li.inflate(, this, true);

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);

Why is this happening? what should be done?

preguntado el 09 de marzo de 12 a las 13:03

1 Respuestas

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.

respondido 09 mar '12, 14:03

if i use it the other way around, i.e. creating an object in every activity i require it in and then setContentView(mMenu) it creates an unwanted delay. Sometimes the delay is so much that the screen goes blank i.e. the old activity goes away and the next hasn't shown up yet. I want to somehow reduce or eliminate that delay. - prometheuspk

@Prometheus87 then you have problems else where in the code if there is a delay. Using a singleton instance will only compound these problems. Have you tries debugging what is causing the delay? There may be something happening you are unaware of - which is probably the case if there is a delay that blacks out the screen. There is really much you can do that would cause this, so something has to be wrong in the code. - jjnford

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.