on every launch of android application, it creates new instances of variables?
<p>I am creating an application in which I want it to beep on change of some preset value accelerometer when toggle switch is on.</p>

<p>But the problem is, if I exit from app when toggle switch is on, and on next launch It beeps twice, and beeps once even if toggle switch is off.</p>

<p>How can I get rid of this issue?</p>

<pre><code>button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub


                if(flg1==false)
                {
                    stat.setText("Status: ON");
                    flg1=true;
                }
                else
                {
                    stat.setText("Status : OFF");
                    flg1=false;
                }

            }
        });




//
public void onSensorChanged(SensorEvent event) {


        deltaX = event.values[0];
        deltaY = event.values[1];
        deltaZ = event.values[2];

//after some changes to delta values


            if(flg1==true &amp;&amp;(deltaX&gt;=Thresh||deltaY&gt;=Thresh||deltaY&gt;=Thresh))
            {
                if(mp1.isPlaying())
                {
                    //do nothing
                }
                else
                {
                    mp1.start();
                    Toast.makeText(getApplicationContext(), "playing beep", Toast.LENGTH_SHORT).show();
                }

            }
</code></pre>
