title: When exactly is it leak safe to use (anonymous) inner classes?
body: <p>I have been reading some articles on memory leaks in Android and watched this interesting video from Google I/O <a href="http://www.youtube.com/watch?v=_CruQY55HOk">on the subject</a>.</p>
  
  <p>Still, I don't fully understand the concept, and especially when it is safe or dangerous to user <strong>inner classes inside an Activity</strong>.</p>
  
  <p>This is what I understood:</p>
  
  <p>A memory leak will occur if an instance of an inner class survives longer than its outer class (an Activity).
  -> <strong>In which situations can this happen?</strong> </p>
  
  <p>In this example, I suppose there is no risk of leak, because there is no way the anonymous class extending <code>OnClickListener</code> will live longer than the activity, right?</p>
  
  <pre><code>    final Dialog dialog = new Dialog(this);
      dialog.setContentView(R.layout.dialog_generic);
      Button okButton = (Button) dialog.findViewById(R.id.dialog_button_ok);
      TextView titleTv = (TextView) dialog.findViewById(R.id.dialog_generic_title);
  
      // *** Handle button click
      okButton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
              dialog.dismiss();
          }
      });
  
      titleTv.setText("dialog title");
      dialog.show();
  </code></pre>
  
  <p>Now, is this example dangerous, and why?</p>
  
  <pre><code>// We are still inside an Activity
  _handlerToDelayDroidMove = new Handler();
  _handlerToDelayDroidMove.postDelayed(_droidPlayRunnable, 10000);
  
  private Runnable _droidPlayRunnable = new Runnable() { 
      public void run() {
          _someFieldOfTheActivity.performLongCalculation();
      }
  };
  </code></pre>
  
  <p>I have a doubt regarding the fact that understanding this topic has to do with understanding in detail what is kept when an activity is destroyed and re-created.</p>
  
  <p>Is it?</p>
  
  <p>Let say I just changed the orientation of the device (which is the most common cause of leaks). When <code>super.onCreate(savedInstanceState)</code> will be called in my <code>onCreate()</code>, will this restore the values of the fields (as they were before orientation change)? Will this also restore the states of inner classes?</p>
  
  <p>I realize my question is not very precise, but I'd really appreciate any explanation that could make things clearer.</p>