Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Couple of design issues with this, but overall it seems reasonable.</p> <p>I would forgo the static instance in the <code>ChildActivity</code> class. Why? Well, think about the relationship you're modeling. A <code>TabActivity</code> <em>has a</em> <code>ChildActivity</code>. This is textbook composition, and would be accomplished by adding a ChildActivity field to the TabActivity class, like so:</p> <pre><code> public class TabActivity { private ChildActivity child; //remember to initialize child in onCreate //then, call methods using child.changeUI();, for example } </code></pre> <p>This is better, because A) now I can have multiple instances of TabActivity and ChildActivity that won't interfere with each other (before, it was just a static variable, so only one ChildActivity could be used), and B) the ChildActivity is encapsulated inside the TabActivity class... before, it was a public field, meaning anything can use and modify it (might not be desirable; can often lead to some strange runtime bugs as well as messy, tied-together code) - we changed it to a private field, because we don't really want other classes accessing it in unexpected ways.</p> <p>The only thing you may need access to from the ChildActivity is the parent (TabActivity). To do this, add the following methods and field to the ChildActivity class, and call the registerParent() method after constructing the ChildActivity:</p> <pre><code>public class ChildActivity ...{ private TabActivity parent; public void registerParent(TabActivity newParent){ if (newParent != null){ parent = newParent; } } } </code></pre> <p>So, if you need access to the parent <code>TabActivity</code> from the child, just call parent.someMethod();</p> <p>It would also be wise to access fields like <code>parent</code> and <code>child</code> through getters and setters; it might save you some time in the long run.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload