title: Keep Activity running in background
body: <p>I have a counter app. It counts things over a long period of time(2-3hrs). During the majority of the time, for 10-15, maybe 30 minute increments), the app remains in power down state in my pocket. After this long duration Android decides i am not using it and shuts it down. So i lose all my counts.</p>
  
  <p>What is best way to keep activity open in background? I was noticing the use of either wakelock, which I don't want to do because of unnecessary battery drain, and services, which would be good, i have no problem operating a service in the background. </p>
  
  <p>Can services update particular interface objects of the activity, basically can it update the count on my counter screen?</p>
  
  <p>and will the service prevent android from shutting down my app? I assume i can battle this by doing something like this...</p>
  
  <pre><code>@Override
  public void onCreate(){
      ....
      if(myService.isRunning())
          Intent i = new Intent(this, MyCounterActivity.class);
          startActivity(intent);
  }
  </code></pre>
  
  <p>That should start my activity back if android shuts it down while running the service. But then how can i get info from the service back to the counter visuals?</p>
  
  <p><em>Edit</em>
  I decided to go with the sharedPreferences recommendation. so to do this i would need something like the following?</p>
  
  <p>SharedPreference Singleton</p>
  
  <pre><code>public class MySharedPreference {   
      private static MySharedPreference mySharedPreference;
      private SharedPreferences sharedPreferences;
  
      public static MySharedPreference getInstance(Context context) {
          if (mySharedPreference == null) {
              mySharedPreference = new mySharedPreference(context);
          }
          return mySharedPreference;
      }
  
      private mySharedPreference(Context context) {
          sharedPreferences = context.getSharedPreferences(MyConstants.PREF_FILE_KEY, Context.MODE_PRIVATE);
      }
  
      public void saveData(String key,String value) {
          SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
          prefsEditor .putString(key, value);
          //prefsEditor.commit(); 
          prefsEditor.apply();
      }
  
      public String getData(String key) {
          if (sharedPreferences!= null) {
             return sharedPreferences.getString(key, "");
          }
          return "";         
      }
  }
  </code></pre>
  
  <p>Activity.class</p>
  
  <pre><code>@Override
  public void onCreate(){
  
  
      SharedPreferences.OnSharedPreferenceChangeListener listener;
      listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
      public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
          if(key.compareTo(MyConstants.PREF_FIELD_M20_COUNT)){
              m20CountTextView.setText(String.format("$1%d", prefs.getInt(key, 0)));
          }else if(key.compareTo(MyConstants.PREF_FIELD_M30_COUNT)){
              m30CountTextView.setText(String.format("$1%d", prefs.getInt(key, 0)))
          }
        }
      };
  
      MySharedPreference.getInstance().registerOnSharedPreferenceChangeListener(listener);
  
      button1 = (Button) findViewById(R.id.button1);
      button1.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View arg0) {
              m20Count++;
              MySharedPreferences.getInstance().saveData(MyConstants.PREF_FIELD_M20_COUNT, m20Count);
          }
  
      });
  
      button2 = (Button) findViewById(R.id.button2);
      button2.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View arg0) {
              m30Count++;
              MySharedPreferences.getInstance().saveData(MyConstants.PREF_FIELD_M30_COUNT, m30Count);
          }
      });
  }
  
  @Override
  public void onResume(){
      updateInterfaceWithPreferences();
  }
  
  private void updateInterfaceWithPreferences(){
      m20CountTextView.setText(String.format("$1%d", MySharedPreferences.getInstance().getInt(MyConstants.PREF_FIELD_M20_COUNT, 0)));
      m30CountTextView.setText(String.format("$1%d", MySharedPreferences.getInstance().getInt(MyConstants.PREF_FIELD_M30_COUNT, 0)));
  }
  </code></pre>
  
  <p>Will this work to save my data and restore it when re-entering the activity that gets shutoff.</p>