title: Global variable persists even when app is closed
body: <p>I have declared a class extending Application to keep a global variable between activities of my app. When I press back from my launcher activity (FirstActivity.Java) my app closes, but the value of variable still remains same, which i do not want. How to achieve it?</p>
  <blockquote>
  <p>Please note - FirstActivity extends BaseActivity</p>
  <p>I am creating this whole setup to show a <strong>pin activity</strong> that asks for pin whenever app starts or comes to foreground from backGround. So I have setup flag to false in <code>onStop()</code> so that pin shows up when it checks the flag in <code>onStart();</code></p>
  </blockquote>
  <p>I did following -</p>
  <p><strong>BaseActivity.Java</strong></p>
  <pre><code>public class BaseActivity extends Activity {
      //static boolean flag = true;
      Context con;
      private ApplicationActivity app;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          //this.savedInstanceState = savedInstanceState;
          app = (ApplicationActivity)getApplication();
          Log.d(&quot;pin&quot;, &quot;on Create base&quot;);
          con = this;
      }
      @Override
      protected void onStart() {
          // TODO Auto-generated method stub
          super.onStart();
          Log.d(&quot;pin&quot;, &quot;on start 1&quot;);
          if(app.isFlag()){
              //Start pin activity here - if pin is true it sets flag to false  
          app.setFlag(false);
          }
  
      }
  </code></pre>
  <p><strong>ApplicaitonActivity.java</strong></p>
  <pre><code>public class ApplicationActivity extends Application{
      private boolean flag = true;
  
      public boolean isFlag() {
          //SharedPreferences sp = new SharedPreferences();
          return flag;
      }
  
      public void setFlag(boolean flag) {
          this.flag = flag;
      }
  </code></pre>
  <p><strong>FirstActivity.Java</strong></p>
  <pre><code>public class MainActivity extends BaseActivity {
  //private Bundle savedInstanceState;
      Context con;
      Button nextActivity;
          
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_main);
          //this.savedInstanceState = savedInstanceState;
          Log.d(&quot;pin&quot;, &quot;on Create 1&quot;);
          con = this;
      }
  }
  </code></pre>