How to use Shared Preferences in Processing android

edited February 2017 in Android Mode

Hello How do i use share preferences to store an integer in processing for android. I don't have any libraries installed and when i say import android.content.SharedPreferences it gives me an error. Is there a function that is called when the program quits so I can update the stored integer in there? And how would i do that? And is there also a function that is called when the program starts? How can i update a variable from the value stored in the shared preferences. Thank you very much. Dan4o

Answers

  • Thank you for the link but I couldn't find anything that can help me.

  • Please show your code to show your approach. If you just want to store a single value, you could write it to a file and read it when the app starts again. You can use it onStart() and onStopped() functions.

    https://developer.android.com/guide/components/activities/activity-lifecycle.html

    Kf

  • Thanks for you reply,

    I Have a highscore float that is being updated every frame in the draw loop and I want to store this when onStopped() runs. I don't care how I'm storing it as long as the value can be accessed in the void setup () when the app runs next.

    I could use an image to store the values but is this an efficient way of doing it? How do I, as you said, store the float value in a text file when the program quits, and in setup assign that variable to another float variable?

    Thank you Dan4o

  • I could also use createWriter and createReader

  • @dan4o===

    SharedPreferences is the best way to do that; once created you add your values when the app is pausing (not stopped!) and you get your values with onResume() please put the code you are using for SharedPreferences and the error code you get.

  • edited February 2017

    Yes I thought that shared preferences is the best way to do it too. As I am very new to coding could you please check my code. All I want it to do is save the highscore value in shared preferences but it didn't work.

        int highscore;
    
        void onResume() {
          SharedPreferences settings;
        settings = context.getSharedPreferences(PREFS_NAME,0); 
            highscore = settings.getInt('score', null); 
        }
    
        void setup () {
          size(displayWidth, displayHeight);
        }
    
        void draw() {
          text(highscore, 10,10);
        }
    
        Void mousePressed(){
        highscore++;
        }
    
        void onPause(){
          SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
           SharedPreferences.Editor editor = settings.edit(); 
           editor.clear();
           editor.putInt("score", highscore); 
           editor.commit();
        }
    

    Thank you.

  • To format your code in the forum, you select your code and you hit ctrl+o. Ensure there is a line above and below your code. You can edit your post by clicking on the gear on the top right side of your post.

    Did you get an error when running your code? The problem is similar to the issue in this recent post: https://forum.processing.org/two/discussion/comment/88438/#Comment_88438

    Focus in lines 70, 71 as it is the proper way to get access to the context of your app:

      act = getActivity();
      mc = act.getApplicationContext();
    

    Kf

  • edited February 2017 Answer ✓

    @dan4o===

    your code cannot work because there are (too...) many errors:

    • you are in a fragment, so instead of (e.g) what you write line 20 you have to write: SharedPreferences settings =this.getActivity(). getSharedPreferences(PREFS_NAME,0);

    • you never initialize context you use line 3

    • you have to create your "settings" in setup() (or in onCreate());

    • onResume() or onPause() must call super.onResume(), super.onPause()

    • calling onResume BEFORE the SharedPreferences object is created must fire an java exception (null object)

    so you have to add a condition like that:

            if(settings !=null){//au lancement settings est null....
                highscore = settings.getInt("score", 0); 
            }
    
    • i think also it could be useful to create 2 methods for saving or loading your highscore.
  • Thank you for all your help. I have finally figured it out. For anyone that wishes to save an integer with shared Preferences in processing, you can use these two functions:

    void saveInt(int _score_, String name){
      SharedPreferences sharedPreferences;
      SharedPreferences.Editor editor;
      Activity act;
      act = this.getActivity();
      sharedPreferences = PreferenceManager.getDefaultSharedPreferences(act.getApplicationContext());
      editor = sharedPreferences.edit();
      editor.putInt(name, _score_);
      editor.commit();
    }
    
    int loadInt(String name){
      SharedPreferences sharedPreferences;
      Activity act;
      act = this.getActivity();
      sharedPreferences = PreferenceManager.getDefaultSharedPreferences(act.getApplicationContext());
      int getScore = sharedPreferences.getInt(name, 0);
      return getScore;
    }
    

    make sure to import at the top of the sketch:

    import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.content.Context; import android.app.Activity;

    To save the value when you exit the app use:

    void onPause(){
      super.onPause();
      saveInt(score, "score1");
    }
    

    and to load it in setup use:

    int score;
    
    void setup () {
      fullScreen();
      score = 0;
      score = loadInt("score1");
    }
    

    I hope this helps anyone with the same problem as me. Thank you for everyone that helped

    Dan4o

Sign In or Register to comment.