title: Reviving activity state in Android
body: <p>I want to write an android app which logs all the photos taken by making an entry into a text file with the photos uri, time-stamp and geo location stamp. This Should happen as and when a photo is clicked. <br/>
  For this I'm running a Service which uses a FileObserver on the default photos directory. (I know this is not fool proof). <br/></p>
  
  <p>The user is initially greeted by a GUI which will let him select a file name and a start button to start recording. When the user presses start recording, the background Service is started and the user goes back to take some photos,1 when he comes back he should have an option to stop recording which ends the background service.</p>
  
  <p>Now my problem is this,<br/> 
  1. how does the activity know when the service is running and when it is not? <br/>
  2. How does the previous state of the Activity get revived and reconnect to the particular service? As in how does the association of the Activity to Service happen when i revive the Activity? (consedring some kind of association is necessary if my Activity has to stop the Service)</p>
  
  <p>Here is my code for reference : [ExperienceLoggerService is an inner class of MainActivity]</p>
  
  <pre><code>public class ExperienceLoggerService extends Service
  /* This is an inner class of our main activity, as an inner class makes good use of resources of outer class */
  {
  
  
      private final IBinder mBinder = new LocalBinder();      
      File file;
      FileOutputStream fOut;
      OutputStreamWriter fWrite;
      /** Called when the activity is first created. */
      private void startLoggerService() 
      {
          try
          {
              //initialise the file in which to log
              this.file = new File(Environment.getExternalStorageDirectory(), "MyAPPNostalgia");
              System.out.println("1:"+Environment.getExternalStorageDirectory()+ "MyAPPNostalgia");
              file.createNewFile();
              fOut = new FileOutputStream(file);
              fWrite = new OutputStreamWriter(fOut);
          }
          catch(Exception e)
          {
              System.out.println("Error in logging data, blame navjot");
          }
          FileObserver observer = new MyFileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA");
          observer.startWatching(); // start the observer
      }
  
      @Override
      public void onCreate() 
      {
          super.onCreate();
          //mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  
          startLoggerService();
  
          // Display a notification about us starting. We put an icon in the
          // status bar.
          //showNotification();
      }
  
      public void onDestroy() 
      {
          try
          {
              //close the file, file o/p stream and out writer.
              fWrite.close(); 
              fOut.close();
          }
  
          catch(Exception e)
          {
          }
  
          super.onDestroy();
  
      }
  
      class MyFileObserver extends FileObserver
      {
          public MyFileObserver(String path) 
          {
              super(path);
          }
  
          public void onEvent(int event, String file) 
          {
              if(event == FileObserver.CREATE &amp;&amp; !file.equals(".probe"))
              { // check if its a "create" and not equal to .probe because thats created every time camera is launched
                  String fileSaved = "New photo Saved: " + file +"\n";
                  try
                  {
                      ExperienceLoggerService.this.fWrite.append(fileSaved);
                  }
                  catch(Exception e)
                  {
                      System.out.println("Problem in writing to file");
                  }
              }
          }
      }
  
  
      @Override
      public IBinder onBind(Intent intent) 
      {
          return mBinder;
      }
  
      public class LocalBinder extends Binder 
      {
          ExperienceLoggerService getService() 
          {
              return ExperienceLoggerService.this;
          }
      }
  
  }
  </code></pre>