Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

using a same view more than once

am i right when i assume that only one instance of a view can be visible?

when i try to place the same view twice in a window, even when inside of different containers, i can only see it in the first container…

thanks

— asked November 18th 2010 by marcin kolonko
  • android
  • instance
  • multiple
  • view
0 Comments

2 Answers

  • Accepted Answer

    On Android more specifically, a view may be attached in the view hierarchy only ONCE. It will throw an exception.

    — answered November 18th 2010 by Don Thorp
    permalink
    1 Comment
    • Ah, OK, thanks, Don. So, the view is not allowed to be added more than once and, if it is, an exception will be thrown (under the hood). This is not exposed to us on the JS side though, Marcin, but, as Don points out, it's better simply to only add it a view once irrespective of whether it is physically possible to do otherwise.

      — commented November 18th 2010 by Paul Dowsett
  • Marcin

    Yes, that is expected - only one instance of an individual object can appear on screen at a time. You can create the effect of a replica of an object, but if you capture the click using an event to output source property value ( ie T.API.info('You just clicked: '+e.source) ), you will see that it's actually a new/different proxy.

    I hope this makes sense. :)

    — answered November 18th 2010 by Paul Dowsett
    permalink
    4 Comments
    • very much. thanks!
      how do you create the effect?

      — commented November 18th 2010 by marcin kolonko
    • Normally you wouldn't want to - it's an undesirable effect of sloppy coding.

      It relates to creating a child window configured with vertical layout outside of an event listener (ie click event), and opening that window inside of the event listener. When that window opens, a view is added to the child window. Then the child window is closed and opened again via the click event. In that child window, two identical views will be displayed, although they are actually unique, separate view proxies.

      This happens because the child window was not destroyed when the event ends, so it still exists. Thus running the event subsequent times simply opens the same window and adds a new view to it.

      To be honest, I explain it simply so you can avoid it! :)

      — commented November 18th 2010 by Paul Dowsett
    • OK, maybe I was being a bit rash. There may be use cases where this behaviour is required - it's just that I have not come across them yet. :)

      — commented November 18th 2010 by Paul Dowsett
    • haha, no, actually i agree. this is too big a hack to be called good coding practice… :D

      — commented November 18th 2010 by marcin kolonko
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.