title: Stop app loading by itself after unlocking device?
body: <p>I am currently creating an Android app that appears to re-open by itself after locking and unlocking the screen of the device.</p>
  
  <p>I'm using a BroadcastReceiver to detect when the power button is pressed. When the power button is detected, an intent is created that should return the user to the login page in the app.</p>
  
  <p>I believe that this is causing the problem but am unsure of how to fix it. I believe that when the user unlocks the device, the intent is followed through and therefore the user is presented with the login screen, regardless of what screen or app they were viewing prior to locking the device. Please note that this only happens if my app is still running in the background.</p>
  
  <p>Below I have included my BroadcastReceiver code.</p>
  
  <pre><code>public class ScreenReceiver extends BroadcastReceiver {
  
      public static boolean wasScreenOn = true;
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
              Intent n = new Intent(context, MainActivity.class);
              context.startActivity(n);
              wasScreenOn = false;
          } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
              wasScreenOn = true;
          }
      }
  
  }
  </code></pre>
  
  <p>Hopefully somebody can help me with this.</p>