Search code examples
androidvariablesfeedbackpreference

Variables In PreferenceActivity


In my android application i want to make a feedback dialog that will show the second time the application is started. How can i do this ? Can i do it by variables in a PreferenceActivity. If the a variable in the preference activity is edit by feks ++; will this be the result of the variable next time the app is started ?

Edit: I dont get any of the suggested answers to work, can i create a text file on the ext or internal store the first time the app is started and check if the file exists ?


Solution

  • Use SharedPreferences:

    public class MainActivity extends Activity {
      private SharedPreferences mSharedPrefs;
      private static final String PREF_LAUNCH_COUNTER = "launch_counter";
      private int mLaunchCount = 0;
      @Override
      public void onCreate(Bundle savedState) {
        mSharedPrefs = getPreferences(Context.MODE_PRIVATE);
        if (savedState != null) {
          mLaunchCount = savedState.getInt(PREF_LAUNCH_COUNTER, 1);
        } else {
          mLaunchCount = mSharedPrefs.getInt(PREF_LAUNCH_COUNTER, 1);
          if(mLaunchCount > 1) {
            //code to handle when the app was launched after the first time.
          } else {
            //code for when the app was launched for the first time..
          }
          mSharedPrefs.edit().putInt(PREF_LAUNCH_COUNTER, mLaunchCount++);
    
        }
    
      }
      @Override
      protected void onSaveInstanceState(Bundle outState) {
         outState.putInt(PREF_LAUNCH_COUNTER, mLaunchCount);
      }
    
    }