title: Android: using getIntent() only within onCreate?
body: <p>In Android (targeting APIs 14-16) I have a <code>MainActivity</code> and a <code>NextActivity</code>. There is no difficulty using intents to start <code>NextActivity</code> from within <code>MainActivity</code> if the <code>getIntent()</code> method is called inside the <code>onCreate()</code> block of <code>NextActivity</code>:</p>
  
  <pre><code>public class MainActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          int data = 7;
          ...
          Intent intent = new Intent(this, NextActivity.class);
          intent.putExtra("data", data);
          startActivity(intent);
          }
      }
  
  public class NextActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          final int data = this.getIntent().getIntExtra("data", 7);
          ...
          }
      ...
      }
  </code></pre>
  
  <p>However, since the field <code>data</code> is being used inside an anonymous ("inner") class in <code>NextActivity</code>, I am compelled to declare it <code>final</code>. </p>
  
  <p>I'd prefer not to declare fields <code>final</code>, and I can usually avoid doing so if I declare them at the beginning of the class, before <code>onCreate()</code> begins. But for some reason, the app crashes when <code>NextActivity</code> starts if the <code>getIntent()</code> statement appears (without the <code>final</code> keyword) outside of <code>onCreate()</code>.</p>
  
  <p>Any idea why?</p>