title: What is the lifecycle for Android's StrictMode?
body: <p>I'm trying to minimize the number of places in my code where I need to setup StrictMode. But I'm not sure if I'm right or not about the following.</p>
  
  <p>The documentation for Android's StrictMode says you can use it for Applications, Activities and other components. I've read that it is not desirable to extend the Application class, and I'd prefer not to extend Application just to enable StrictMode. But I don't think I have to.</p>
  
  <p>There are two policies you can use: ThreadPolicy (for a thread) and VmPolicy (for all threads). So it would seem that if I setup StrictMode on a thread once, it doesn't matter from where I do that, and violations will be reported thereafter on that thread regardless of other calls or not on StrictMode. I just need to call it from somewhere before violations could occur that I want to detect. And it needs to be setup for any new threads that get created in my application that I also want to check.</p>
  
  <p>What I think I want to avoid is calling the build() methods more than I need to. Putting StrictMode at the beginning of <code>onCreate()</code> in all my activities means that build() is going to get called more than once on that thread. If I have one Launcher activity in my application, setting up StrictMode in that activity's <code>onCreate()</code> should be sufficient for the rest of the application. Is that true?</p>
  
  <p>Secondly, if my main activity gets restarted even though the application did not die, is it technically necessary to call StrictMode again? Or is my thread still setup to report violations? I was thinking there might be some value in doing a wrapper-type of class around StrictMode like so:</p>
  
  <pre><code>public class MyStrictModeSettings {
      static private List&lt;Long&gt; setThreads = new ArrayList&lt;Long&gt;();
  
      // Prevent instantiation of this class
      private MyStrictModeSettings() {}
  
      static public synchronized void init() {
          try {
              Long tid = Thread.currentThread().getId();
              if(!setThreads.contains(tid)) {
                  setThreads.add(tid);
                  Class sMode = Class.forName("android.os.StrictMode");
                  Method enableDefaults = sMode.getMethod("enableDefaults");
                  enableDefaults.invoke(null);
              }
          }
          catch(Exception e) {
              // StrictMode not supported on this device, punt
              Log.v("StrictMode", "... not supported. Skipping...");
          }
      }
  }
  </code></pre>
  
  <p>That way, in my main activity's onCreate(), I can simply call MyStrictModeSettings.init() and be done with it. And it should work on Android versions prior to 2.3 too. But it may not be worth it. Brad, are you there? Thanks.</p>
  
  <p><strong>Edit:</strong> Since VmPolicy is for all threads, technically I only need to set that up once per application, right? So enableDefaults() is wasting effort to redo the VmPolicy when it gets called a second, third, etc time? Again, maybe it's more trouble than it's worth to try to avoid the extra calls.</p>