title: Backing out of a fragment or activity when a button is pressed
body: <p>I have a contact list that takes the user to a contact edit screen. I am trying to handle closing of the edit activity/fragment (contact edit) depending on which one I am currently using. So if I'm using a fragment, I'd like it to close the fragment after saving or deleting from the database, but the same goes for when I'm in portrait/landscape mode on a small device and running an activity. I have the following code that pops up an error (as I expected) when I'm in twopane mode. Basically, I want the user to see a list of contacts, edit the contacts, then see the updated list of contacts plus or minus their edits. </p>
  
  <pre><code>// Set the save button to check the required fields, save the contact and finish
  saveButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      if (TextUtils.isEmpty(mLastName.getText().toString()) ||
              TextUtils.isEmpty(mFirstName.getText().toString())) {
        makeToast();
      } else {   
          FragmentManager fragmentManager = getFragmentManager();
          fragmentManager.popBackStackImmediate();
          getActivity().finish();
     }
    }
  });
  
  // Set the delete button to delete the contact and close the fragment
  deleteButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
          // Delete the contactUri created from an ID passed in from contactActivity
          getActivity().getContentResolver().delete(contactUri, null, null);
  
          FragmentManager fragmentManager = getFragmentManager();
          fragmentManager.popBackStackImmediate();
          getActivity().finish();
      }
    });  
  return aView;
  </code></pre>
  
  <p>With the above code I get a NullPointerException at contactEditFragment.onClick</p>
  
  <p>I guess I need a way to differentiate between an activity and a fragment.</p>
  
  <pre><code>05-05 05:40:57.200: E/AndroidRuntime(1350): FATAL EXCEPTION: main
  05-05 05:40:57.200: E/AndroidRuntime(1350): java.lang.NullPointerException
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at com.somestuff.ContactEditFragment$1.onClick(ContactEditFragment.java:106)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at android.view.View.performClick(View.java:4204)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at android.view.View$PerformClick.run(View.java:17355)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at android.os.Handler.handleCallback(Handler.java:725)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at android.os.Handler.dispatchMessage(Handler.java:92)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at android.os.Looper.loop(Looper.java:137)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at android.app.ActivityThread.main(ActivityThread.java:5041)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at java.lang.reflect.Method.invoke(Method.java:511)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  05-05 05:40:57.200: E/AndroidRuntime(1350):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Here is the onContactSelected:</p>
  
  <pre><code>public void onContactSelected(String id) {
          if (mTwoPane)
          {
              Log.i("SECOND INSIDE OF ONITEMSELECTED TWO PANE MODE !!! IN CONTACTS ACTIVITY - THE ID IS:", id);
              // In two-pane mode, show the contact edit view in this activity by
              // adding or replacing the detail fragment using a
              // fragment transaction.
  
                  // Create new fragment and transaction
                  Bundle arguments = new Bundle();
                  arguments.putString(ContactEditFragment.CONTACT_ID, id);
                  ContactEditFragment contactEditFragment = new ContactEditFragment();
                  contactEditFragment.setArguments(arguments);
                  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  
                  // Replace whatever is in the fragment_container view with this fragment,
                  // and add the transaction to the back stack
                  transaction.replace(R.id.item_detail_container, contactEditFragment);
                  transaction.addToBackStack(null);
  
                  // Commit the transaction
                  transaction.commit();
  
          } else {
              // In single-pane mode, simply start the detail activity
              // for the selected contact ID.
              Intent detailIntent = new Intent(this, ContactEditActivity.class);
              detailIntent.putExtra(ContactEditFragment.CONTACT_ID, id);
              startActivity(detailIntent);
                  }
  </code></pre>