Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public static final String MyPREFERENCES = "MyPrefs" ;
  2. public static final String counterKey = "counterKey";
  3.  
  4. SharedPreferences sharedpreferences;
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. //doing basic stuff of set layout and all here...
  10.  
  11. //Initialize sharedpreferences here
  12. sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
  13.  
  14. //call to increment counter here
  15. updateAppOpenCounter();
  16.  
  17. //do remaining stuff here....
  18. }
  19.  
  20. private void updateAppOpenCounter() {
  21. SharedPreferences.Editor editor = sharedpreferences.edit();
  22.  
  23. editor.putInt(counterKey, getAppOpenedCount() + 1);
  24. editor.commit();
  25. }
  26.  
  27. private int getAppOpenedCount() {
  28. sharedpreferences.getInt(counterKey, 0); //Kept 0 as default opened count if not set to shared yet.
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement