Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Often what is done is subclassing the Application class, which is guaranteed to have only one instance.</p> <p>Application subclass: </p> <pre><code>public class MyApplication extends Application { private static Context mContext; @Override public void onCreate(){ super.onCreate(); mContext = this; } public static Context getContext(){ return mContext; } } </code></pre> <p>Your class:</p> <pre><code>public String compassOrdinalForBearing(float bearing) { Context context = MyApplication.getContext(); String north = context.getResources().getString(R.string.compass_ordinal_north);) } </code></pre> <p>But don't forget to change the Manifest:</p> <pre><code>&lt;application android:name="com.example.myapp.MyApplication"&gt; </code></pre> <p>Alternatively you can just pass in a context during instantiation, without keeping a pointer to it, as almost surely these objects will be instantiated from a context. </p> <pre><code>private Static string NORTH = null; public MyClass(Context context){ initializeDirections(context); } private static void initializeDirections(Context context){ if(NORTH == null){ NORTH = context.getResources().getString(R.string.compass_ordinal_north); } } </code></pre> <p>Finally a kind of messy combination of the two just in case you genuinely cannot pass in a context upon instantiation and you do not want to keep the application context in the Application subclass:</p> <pre><code>public class MyApplication extends Application { @Override public void onCreate(){ super.onCreate(); MyClass.initializeDirections(this); } } public class MyClass{ private static String NORTH = null; public static final void initializeDirections(Context context){ NORTH = context.getResources().getString(R.string.compass_ordinal_north); } } </code></pre> <p>Edit: On an unrelated note, at least from only this only snippet, you can kill all of the first conditionals. If it has reached that particular 'else', the first conditional is necessarily true. </p> <p>For example:</p> <pre><code>else if ((bearing &gt;= 0) &amp;&amp; (bearing &lt;= 22.5)) return "North"; else if ((bearing &gt; 22.5) &amp;&amp; (bearing &lt;= 67.5)) return "Northeast"; </code></pre> <p>Could be:</p> <pre><code>else if (bearing &lt;= 22.5) return "North"; else if (bearing &lt;= 67.5) return "Northeast"; </code></pre> <p>As if <code>bearing</code> is !&lt;= 22.5, it is necessarily > 22.5. </p> <p>This may or may not improve readability to you and may or may not be desirable to you. Just some probably unwanted two cents :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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