title: Android app becomes unresponsive when back button is pressed and crashes on android emulator
body: <p>My app consists of two activities.In the first activity some input is taken from the user and passed to the second activity through intent. The second activity starts two <code>IntentServices</code> to perform some long-running background tasks. I have a <strong>stop</strong> button which stops the services. When I try to press the back button after stopping the services, the previous activity loads but the app becomes unresponsive and closes down. I tried to solve the problem by going through suggestions on this site but still the problem persists.</p>
  
  <p><strong>code for starting 2nd activity from 1st</strong></p>
  
  <pre><code>Intent intent = new Intent(ConnectActivity.this, MessageActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  intent.putExtra("ip", ip);
  intent.putExtra("port", port);
  intent.putExtra("tcpport", tcpport);
  intent.putExtra("tcpip", tcpip);
  intent.putExtra("packetType", PT);
  startActivity(intent);
  </code></pre>
  
  <p><strong>code for <code>backButtonPressed()</code> from 2nd activity</strong></p>
  
  <pre><code>public void onBackPressed() {
      if(UdpSendService.send)
      while(!endTransmission());
      //Intent intent = new Intent(this, ConnectActivity.class);
      //intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
      //startActivity(intent);
      finish();
      //super.onBackPressed();
  }
  </code></pre>
  
  <p><strong>the <code>endTransmission()</code> method</strong></p>
  
  <pre><code>boolean endTransmission() {
      UdpSendService.send=false;
      if(receiver1!=null &amp;&amp; receiver2!=null){
          unregisterReceiver(receiver1);
          unregisterReceiver(receiver2);
          receiver1=null;
          receiver2=null;
      }
      while(stopService(udpReceiveIntent)!=true);
      System.out.println("\nreceive service stopped");
      while(stopService(udpServiceIntent)!=true);
      System.out.println("\nsend service stopped");
      return true;
  }
  </code></pre>
  
  <p><strong>logcat:</strong></p>
  
  <pre><code>01-22 15:18:39.893: E/ActivityManager(162): ANR in com.example.udpmessageclient (com.example.udpmessageclient/.ConnectActivity)
  01-22 15:18:39.893: E/ActivityManager(162): Reason: keyDispatchingTimedOut
  01-22 15:18:39.893: E/ActivityManager(162): Load: 1.39 / 1.05 / 0.66
  01-22 15:18:39.893: E/ActivityManager(162): CPU usage from 12723ms to 0ms ago:
  01-22 15:18:39.893: E/ActivityManager(162):   63% 162/system_server: 53% user + 9.6% kernel / faults: 593 minor
  01-22 15:18:39.893: E/ActivityManager(162):   35% 1467/com.example.udpmessageclient: 27% user + 7.9% kernel / faults: 44 minor
  01-22 15:18:39.893: E/ActivityManager(162):   0.1% 293/com.android.phone: 0.1% user + 0% kernel / faults: 4 minor
  01-22 15:18:39.893: E/ActivityManager(162): 99% TOTAL: 82% user + 17% kernel
  01-22 15:18:39.893: E/ActivityManager(162): CPU usage from 3151ms to 3707ms later:
  01-22 15:18:39.893: E/ActivityManager(162):   58% 162/system_server: 47% user + 10% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     9% 334/Binder_3: 9% user + 0% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     7.2% 173/Binder_1: 5.4% user + 1.8% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     7.2% 174/Binder_2: 7.2% user + 0% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     5.4% 162/system_server: 3.6% user + 1.8% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     5.4% 195/InputDispatcher: 1.8% user + 3.6% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     5.4% 336/Binder_4: 3.6% user + 1.8% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     5.4% 367/Binder_5: 5.4% user + 0% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     5.4% 374/Binder_6: 5.4% user + 0% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     5.4% 380/Binder_7: 3.6% user + 1.8% kernel
  01-22 15:18:39.893: E/ActivityManager(162):     5.4% 383/Binder_8: 3.6% user + 1.8% kernel
  01-22 15:18:39.893: E/ActivityManager(162):   39% 1467/com.example.udpmessageclient: 28% user + 10% kernel / faults: 2 minor
  01-22 15:18:39.893: E/ActivityManager(162):     35% 1467/dpmessageclient: 25% user + 10% kernel
  01-22 15:18:39.893: E/ActivityManager(162): 100% TOTAL: 78% user + 21% kernel
  01-22 15:18:43.783: E/InputDispatcher(162): channel '4119ec38 com.example.udpmessageclient/com.example.udpmessageclient.ConnectActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
  01-22 15:18:43.853: E/InputDispatcher(162): Received spurious receive callback for unknown input channel.  fd=187, events=0x9
  01-22 15:21:45.544: E/Trace(1523): error opening trace file: No such file or directory (2)
  01-22 15:21:46.265: E/ActivityThread(278): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
  01-22 15:21:46.265: E/BinaryDictionaryGetter(278): Could not find a dictionary pack
  01-22 15:21:46.355: E/ActivityThread(278): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
  01-22 15:21:46.355: E/BinaryDictionaryGetter(278): Could not find a dictionary pack
  </code></pre>