title: Android app crashes when back button is pressed
body: <p>I have run into an annoying crash in my app when the user clicks the back button.</p>
  
  <p>In my app, i have 3 relevant activities:</p>
  
  <ul>
  <li>MainActivity: shows a list of Persons</li>
  <li>PersonActivity: shows a list of all the Payments of a Person</li>
  <li>AddPaymentActivity: has input elements to add a Payment to a Person</li>
  </ul>
  
  <p>From MainActivity the user can either tap on a Person's name in the list to go to PersonActivity or use a "+" button next to the Person's name and jump directly into AddPaymentActivity. In PersonActivity there is a "+" button that leads to AddPaymentActivity, too.</p>
  
  <p>The Perons data is stored in a Java Person class that implements the Serializable interface. When starting an Activty i forward the Person Object via</p>
  
  <pre><code>intent.putExtra(MainActivity.PERSON_MESSAGE, person);
  </code></pre>
  
  <p>When i go from MainActivity directly into AddPaymentActivity (via "+"), the back button works fine. If i however use start AddPaymentActivity from PersonActivity, my application crashes with a null pointer exception.</p>
  
  <p>Using the debugger i noticed that PersonActivity.onCreate() is called (again?) when the back button is pressed. Why does this happen when going back form AddPaymentActivity to PersonActivity? And why does that NOT happen when going back from AddPaymentActivity to MainActivity? What can i do to fix the problem?</p>
  
  <p>Stack trace:</p>
  
  <pre><code>01-13 10:11:01.423: E/AndroidRuntime(1022): FATAL EXCEPTION: main
  01-13 10:11:01.423: E/AndroidRuntime(1022): Process: de.emteg.cashsplit, PID: 1022
  01-13 10:11:01.423: E/AndroidRuntime(1022): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.emteg.cashsplit/de.emteg.cashsplit.PersonActivity}: java.lang.NullPointerException
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.os.Handler.dispatchMessage(Handler.java:102)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.os.Looper.loop(Looper.java:137)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.app.ActivityThread.main(ActivityThread.java:4998)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at java.lang.reflect.Method.invoke(Method.java:515)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at dalvik.system.NativeStart.main(Native Method)
  01-13 10:11:01.423: E/AndroidRuntime(1022): Caused by: java.lang.NullPointerException
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at java.math.BigDecimal.&lt;init&gt;(BigDecimal.java:425)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at de.emteg.cashsplit.PersonActivity.onCreate(PersonActivity.java:37)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.app.Activity.performCreate(Activity.java:5243)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
  01-13 10:11:01.423: E/AndroidRuntime(1022):     ... 11 more
  </code></pre>
  
  <p>MainActivity.onCreate:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_overview);
  
      persons = new ArrayList&lt;Person&gt;();
  
              // Populate list with some test person objects
      Person p = new Person("Frank");
      p.getPayments().add(new Payment("test1", new BigDecimal(5.7)));
      persons.add(p);
  
      p = new Person("Peter");
      persons.add(p);
  
      ListView list = (ListView) findViewById(R.id.listView1);
      PersonAdapter adapter = new PersonAdapter();
      list.setAdapter(adapter);
      // output....
  }
  </code></pre>
  
  <p>PersonActivity.onCreate():</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_person);
  
      Intent intent = getIntent();        
      this.person = (Person) intent.getSerializableExtra(MainActivity.PERSON_MESSAGE);
      this.share = new BigDecimal(intent.getStringExtra(MainActivity.SHARE_MESSAGE));
      this.totalSum = new BigDecimal(intent.getStringExtra(MainActivity.SUM_MESSAGE));
      this.diff = person.getSum().subtract(share);
  
      setupActionBar();
  
      // output...
  }
  </code></pre>
  
  <p>AddPaymentActivty.onCreate():</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_add_payment);
      // Show the Up button in the action bar.
      setupActionBar();
  
      Intent intent = getIntent();
  
      this.person = (Person) intent.getSerializableExtra(MainActivity.PERSON_MESSAGE);
  
      if (intent.hasExtra(MainActivity.PAYMENT_MESSAGE)) {
          this.payment = (Payment) intent.getSerializableExtra(MainActivity.PAYMENT_MESSAGE);
          // output....
      } else {
          this.payment = null;
          // output...
      }
  
  }
  </code></pre>