Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Kumar,</p> <p>The behavior you are seeing is the result of using a <code>static</code> member. <code>static</code> members do not require an instance and are therefore set upon first access (regardless of whether an instance was created or not) and stays in memory until Android decides it is no longer valid. In essence, it is doing exactly what it was supposed to. The proper use of static variables is a topic of extensive discussion among veteran and novice programmers alike, but essentially always leads to "be choosy about where and how you use static members".</p> <p>That said, your need to access this from another component is a common problem, however, and there are a number of ways to solve it. If you need to have each instance have a different value, then it should not be static. Instead you will have to find a way to pass the instance of the Activity. </p> <p>If each instance need not be different, but the value need to change according to some other parameter, simply find the appropriate place to change the value. If you can access it from anywhere in your application, you may also change it from anywhere in your application.</p> <p><strong>Solution 1: Passing by Intent</strong></p> <p>This solution is useful when the information is subject to change and must be sent to another component and the classes that it uses exlusively. You may pass virtually any value via an Intent extra.</p> <pre><code>openAlternative.putExtra("MyValue", Integer.parseInt(check)); </code></pre> <p>In your responding component, you may retrieve the value by:</p> <pre><code>Intent myIntent = getIntent(); int n = myIntent.getIntExtra("MyValue", 0); //0 is the default if no value is sent. </code></pre> <p>From here, you may easily pass the retrieved value to any class being utilized by that component. An example:</p> <pre><code>MyClass.setN(n); </code></pre> <p><strong>Solution 2: Storing outside of the LifeCycle</strong></p> <p>A safer alternative is to move the value to an extended Application. This is not subject to UI or LifeCycle processing.</p> <pre><code>public class MyApplication extends Application { static int n; } </code></pre> <p>Adjust your AndroidManifest.xml...</p> <pre><code>&lt;application android:name=".MyApplication" ... &gt; &lt;!-- All of your components --&gt; &lt;/application&gt; </code></pre> <p>Now, you can set the variable this way:</p> <pre><code>MyApplication.n = Integer.parseInt(); </code></pre> <p>And you can get it by </p> <pre><code>int myN = MyApplication.n; </code></pre> <p>This solution has gotten me through many a troubled day. However, it should really be used for non-instance related data.</p> <p><strong>Solution 3: The REALLY UNSAFE method</strong></p> <p>This solution only works if you can guarantee a single instance of the component. This requires that singleTask is set. Be <strong>very</strong> careful with this</p> <p>Change n to non-static</p> <pre><code>int n; </code></pre> <p>Change Activity to Singleton</p> <pre><code>static private Model myInstance; </code></pre> <p>In OnCreate, set myInstance</p> <pre><code>myInstance = this; </code></pre> <p>Create a getter:</p> <pre><code>static public Model getStaticInstance() { return myInstance; } </code></pre> <p>This is unreliable (at best) and can cause huge memory leaks if not managed correctly.</p> <p>Hope this helps,</p> <p>FuzzicalLogic</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