title: How to get result from pending Intent in OnActivityResult of MainActivity?
body: <p>How to get result from pending Intent in <code>OnActivityResult</code> of <code>MainActivity</code> (from where it was created)? I have been trying to use <code>creatependingresult(...)</code> without much success </p>
  
  <p>The approach used is based on <a href="https://stackoverflow.com/q/10362525">this</a> question.</p>
  
  <p>I'm getting a nullpointerException in the onDestroy function at runtime</p>
  
  <pre><code>/*code snippet from MainActivity.java*/
  
          public void processRoute() {
          Intent proximityIntent = new Intent(getApplicationContext(),     ProximityActivity.class);
          getresult = this.createPendingResult(counter, new Intent(),     PendingIntent.FLAG_CANCEL_CURRENT);
          Log.d("get result", "done");
          Parcel p = Parcel.obtain();
          p.writeValue(getresult);
          parcelsend = MyParcel.CREATOR.createFromParcel(p);
  
          proximityIntent.putExtra("attachedIntentParcelable", parcelsend);
          proximityIntent.putExtra("counter", counter);
          Log.d("notif", "intent attached as parcelable");
  
          pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, proximityIntent, PendingIntent.FLAG_CANCEL_CURRENT);
          locationManager.addProximityAlert(orderedList.x.get(counter).x.latitude, orderedList.x.get(counter).x.longitude, 20, -1, pendingIntent);
          Log.d("alert added", "proximity alert added");
  
      }
  </code></pre>
  
  
  
  <pre><code>/*ProximityActivity.java*/
      package anmol.csp5;
  
  import android.app.Activity;
  import android.app.Notification;
  import android.app.NotificationManager;
  import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.location.LocationManager;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.Parcel;
  import android.support.v4.app.NotificationCompat;
  import android.util.Log;
  import android.widget.Toast;
  
  import android.preference.PreferenceManager.OnActivityDestroyListener;
  
  
  
  public class ProximityActivity extends Activity {
  
      String notificationTitle;
      String notificationContent;
      String tickerMessage;
      PendingIntent receive;
      Intent msg;
      int c;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
  
          boolean proximity_entering = getIntent().getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
          MyParcel receivedParcelable = getIntent().getParcelableExtra("attachedIntentParcelable");
          c=getIntent().getIntExtra("counter",-1);
          Parcel rec = Parcel.obtain();
          receivedParcelable.writeToParcel(rec,0);
  
          receive =(PendingIntent) rec.readValue(PendingIntent.class.getClassLoader());
          Log.d("notif", "pending intent unpacked");
  
          if(proximity_entering){
              Toast.makeText(getBaseContext(),"Entering the region"  ,Toast.LENGTH_LONG).show();
              notificationTitle="Proximity - Entry";
              notificationContent="Entered the region";
              tickerMessage = "Entered the region";
              Log.d("alert point","entered");
          }else{
              Toast.makeText(getBaseContext(),"Exiting the region"  ,Toast.LENGTH_LONG).show();
              notificationTitle="Proximity - Exit";
              notificationContent="Exited the region";
              tickerMessage = "Exited the region";
              Log.d("alert point","exited");
          }
  
          msg = new Intent(getApplicationContext(),MainActivity.class);
          msg.putExtra("entered",proximity_entering);
  //        receivedIntent = PendingIntent.getActivity(getApplicationContext(),count,send,PendingIntent.FLAG_CANCEL_CURRENT);
  
  //        try{
  //            receive.send(c);
  //        }
  //        catch (PendingIntent.CanceledException e) {
  //            Log.d("exception","cancelled");
  //            e.printStackTrace();
  //        }
  
          Intent notificationIntent = new Intent(getApplicationContext(),NotificationView.class);
          notificationIntent.putExtra("content", notificationContent );
  
          /** This is needed to make this intent different from its previous intents */
          notificationIntent.setData(Uri.parse("tel:/"+ (int)System.currentTimeMillis()));
  
          /** Creating different tasks for each notification. See the flag Intent.FLAG_ACTIVITY_NEW_TASK */
          PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
  
          /** Getting the System service NotificationManager */
          NotificationManager nManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
  
          /** Configuring notification builder to create a notification */
  
          NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                  .setWhen(System.currentTimeMillis())
                  .setContentText(notificationContent)
                  .setContentTitle(notificationTitle)
                  .setAutoCancel(true)
                  .setTicker(tickerMessage)
                  .setContentIntent(pendingIntent);
  
  
          /** Creating a notification from the notification builder */
          Notification notification = notificationBuilder.build();
  
          /** Sending the notification to system.
           * The first argument ensures that each notification is having a unique id
           * If two notifications share same notification id, then the last notification replaces the first notification
           * */
          nManager.notify((int)System.currentTimeMillis(), notification);
  
          /** Finishes the execution of this activity */
          finish();
  
  
          // OnDestroy method
  //        Intent result=new Intent(getApplicationContext(),MainActivity.class);
  
      }
      @Override
      protected void onDestroy(){
          super.onDestroy();
          try{
              receive.send(getApplicationContext(),c,msg);
              //recei
          } catch (PendingIntent.CanceledException e) {
              Log.d("exception","cancelled");
              e.printStackTrace();
          }
      }
  }
  </code></pre>