1

I have declared a class extending Application to keep some global array variables between activities of my app. When i stop the app and execute it again these values persist which i do not want. So I want to know how to reset these values when the app exits and executes again.

Sanjay Bhatnagar
  • 111
  • 1
  • 1
  • 6
  • When you say "stop the app", do you mean kill the application, or just bring it to the background? – Tamby Kojak Apr 05 '14 at 07:57
  • actually i find that these values persist between - – Sanjay Bhatnagar Apr 05 '14 at 08:03
  • Actually i find that these values persist surprisingly between - first when the user clicks device back button while the main activity is running, second after the user exits the app by clicking an exit button provided by me in the main activity (when i call finish). I want this behavior for the first case but not for the 2nd case - so what do i do to reset these values in this case? ; Also I dont know how to add a CR in comments. – Sanjay Bhatnagar Apr 05 '14 at 08:36

1 Answers1

0

I am suggesting you to use SharedPrefence insted of creating new class and extending Application class.

Using SharedPreferences you can maintain Global data in your app and also remove it whenever you need by just single line of code.

(SharedPrefence)s.edit().clear().commit();
Jay Vyas
  • 2,674
  • 5
  • 27
  • 58
  • Is it possible to use SharedPreference for arrays of ints too? how? – Sanjay Bhatnagar Apr 05 '14 at 08:47
  • ya its very simple Set s= new HashSet s.addAll(yourArray); SharedPrefence.putStringSet("key",s); – Jay Vyas Apr 05 '14 at 09:24
  • ok thanks but it is still not clear as i am completely new to this- shared preferences - so this is better than making a separate class? – Sanjay Bhatnagar Apr 07 '14 at 07:49
  • How can I just reset values in the class declared - I have two int arrays to keep track of and even after exiting app with finish the values persist which is not clear to me. – Sanjay Bhatnagar Apr 07 '14 at 07:56
  • I am not able to find any sample code for above - using SharedPreferences for arrays – Sanjay Bhatnagar Apr 07 '14 at 09:25
  • It seems sharedpreferences work only for strings - so for int or int array int-string-int conversion is required? I dont follow above snippet - Set s= new HashSet s.addAll(yourArray); SharedPrefence.putStringSet("key",s); – Sanjay Bhatnagar Apr 07 '14 at 09:43