title: Check login on every activity
body: <p>I'm developing and app where the user must login before using it. I customized my titlebar and put a logout button there, to enable users to logout whenever they want, and login with another account. The app should then display the login activity again.</p>
  
  <p>That works well, but my problem is with the back button. When the user logs out, the login activity show up, but if he presses back, the app returns to the previous page, enabling the user to use that activity again, even without login.</p>
  
  <p>I have my <code>onCreate()</code> of every activity set up this way:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {  
      // Activity code  
      checkLogin();  
  }
  </code></pre>
  
  <p>And the <code>checkLogin()</code>:</p>
  
  <pre><code>if (GlobalContext.getCurrentUser() == null) {
      Intent i = new Intent(this, LoginActivity.class);
      startActivityForResult(i, GlobalContext.REQUEST_LOGIN);
  }
  </code></pre>
  
  <p>However that's not working like it should, my app crashes sometimes when I press back, sometimes it return to the previous activity like I said, and sometimes it does actually work.</p>
  
  <p>How can I make the <code>checkLogin()</code> get called everytime the activity is (re)started?<br>
  Or there is another, better way to do what I want?</p>
  
  <p>EDIT:</p>
  
  <p>Based on @Gabriel's answer and the following diagram from <a href="http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle" rel="noreferrer">the SDK references</a>, I moved the <code>checkLogin()</code> to the <code>onResume()</code> method instead of the <code>onCreate</code>, solving my problem.</p>
  
  <p><img src="https://i.stack.imgur.com/5YNov.png" alt="Activity life cycle"></p>