# Save int for Android program

**URL:** <https://discourse.processing.org/t/save-int-for-android-program/12092>\
**Category:** Processing for Android\
**Created:** [June 16, 2019, 10:16am UTC](https://discourse.processing.org/t/save-int-for-android-program/12092 "2019-06-16T10:16:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nick1](https://discourse.processing.org/u/Nick1)\
**Post date:** [June 16, 2019, 10:16am UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/1 "2019-06-16T10:16:26Z")

</div>

hey,  
i want that counter is saved when somebody restart the app or something.  
I tried it with a string but idk how i can do it. I think you dont need the pictures.

thanks for help

here the code:

```auto
PImage yo_oli, kind, aldi, shop_innen;
int x;
int counter,level, auswahl;

void setup()
{
  size(displayWidth, displayHeight);
  orientation(LANDSCAPE);
  yo_oli=loadImage("yo_oli2.png");
  kind=loadImage("kind.png");
  aldi=loadImage("aldi.jpg");
  shop_innen=loadImage("shop.jpg");
  x=400;
  level=1;
  auswahl=1;
  
}

void draw()
{
  if (auswahl==1)
  {
    
    image(aldi, 0, 0, displayWidth, displayHeight);
    image(yo_oli, x, 200, 400, 800);
    image(kind, 1000, 140, 300, 400);
    fill(255, 64, 64);
    rect(750, 700, 400, 200);
    rect(1600, 900, 300, 100);
    fill(139, 0, 0);
    textSize(50);
    text("Rein in die Olga", 760, 810);
    text("Olga Counter:" + counter, 720, 50);
    text("Level: " + level, 20, 50);
    text("Shop", 1690, 970);

    if (x>600)
    {
      x=400;
      counter=counter+1;
      
    }

    //LEVEL
    if (counter>20)        
    {
      level=2;
    }

    if (counter>40)        
    {
      level=3;
    }

    if (counter>60)        
    {
      level=4;
    }
    if (counter>80)        
    {
      level=5;
    }
    if (counter>100)        
    {
      level=6;
    }
    if (counter>120)        
    {
      level=7;
    }
    if (counter>140)        
    {
      level=8;
    }
    if (counter>160)        
    {
      level=9;
    }
  }
  if (auswahl==2)
  {

    image(shop_innen, 0, 0, displayWidth, displayHeight);
    fill(255, 64, 64);
    rect(10, 900, 300, 100);
    fill(139, 0, 0);
    textSize(50);
    text("back", 100, 970);
    textSize(100);
     text("Coming soon!", 600, 500);
  }
}

void mousePressed()
{
  println(mouseX, mouseY);
  if (auswahl==1)
  {
    if (mouseX>750 && mouseX<1150 && mouseY>700 && mouseY<900)
    {
      x=x+100;
    }

    if (mouseX>1600 && mouseX<1900 && mouseY>900 && mouseY<1000)
    {
      auswahl=2;
    }
  }

  if (auswahl==2)
  {
    if (mouseX>10 && mouseX<310 && mouseY>900 && mouseY<1000)
    {
      auswahl=1;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [June 16, 2019, 10:50am UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/2 "2019-06-16T10:50:36Z")

</div>

while it might not be easy under android,  
the idea could be to use a file,  
and  
[https://processing.org/reference/loadStrings\_.html](https://processing.org/reference/loadStrings_.html)  
[https://processing.org/reference/saveStrings\_.html](https://processing.org/reference/saveStrings_.html)

* * *

a bigger thing is a general SETTINGS handling, like i try [here](http://kll.engineering-news.org/kllfusion01/articles.php?article_id=154#here16)

---

<div class="post-metadata">

**Author:** ![Nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nick1](https://discourse.processing.org/u/Nick1)\
**Post date:** [June 16, 2019, 12:47pm UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/3 "2019-06-16T12:47:43Z")

</div>

jeah I tried it already but it dont work

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [June 16, 2019, 1:04pm UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/4 "2019-06-16T13:04:24Z")

</div>

in your code i can not see that you tried to store data to a file  
-a- what code  
-b- does it work with processing 3.5.3 on PC with a /data/datafile.txt  
-c- but it does not work under android?

---

<div class="post-metadata">

**Author:** ![Nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nick1](https://discourse.processing.org/u/Nick1)\
**Post date:** [June 16, 2019, 3:21pm UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/5 "2019-06-16T15:21:34Z")

</div>

i had it before but i have a problem with the  
save and loadStrings

That is the reason why im asking in this forum

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [June 16, 2019, 6:30pm UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/6 "2019-06-16T18:30:03Z")

</div>

@Nick1 ===

that is easy; you have to save your ints (when==on onPause) with preferences, that s all

---

<div class="post-metadata">

**Author:** ![Nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nick1](https://discourse.processing.org/u/Nick1)\
**Post date:** [June 16, 2019, 6:42pm UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/7 "2019-06-16T18:42:27Z")

</div>

can you make a exaple for me?  
I dont understand this

but thanks

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 16, 2019, 7:05pm UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/8 "2019-06-16T19:05:36Z")

</div>

Here is a link where `onPause` is described in case you want to know more about [this](https://github.com/processing/processing-android/wiki/Lifecycle-of-a-Processing-sketch).

I did a quick search in the forum:

> [Search results for 'onPause' - Processing Foundation](https://discourse.processing.org/search?q=onPause)

And I recommend this: [super.onSaveInstanceState does not exist - #13 by akenaton](https://discourse.processing.org/t/super-onsaveinstancestate-does-not-exist/3251/13) to start.

You can add the following to your sketch:

```java
// @@@@@@@@@@@@
@Override 
  public void onPause() {
  super.onPause();
}

```

Also, this [link](https://stackoverflow.com/questions/8640821/how-to-save-application-state-on-android) gives a lead to what you could do next. For now, you can use the file approach. You whould be able to use functions `saveStrings()` and `loadStrings()` for this purpose. More [here](https://processing.org/reference/).

Kf

---

<div class="post-metadata">

**Author:** ![Nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nick1](https://discourse.processing.org/u/Nick1)\
**Post date:** [June 16, 2019, 7:31pm UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/9 "2019-06-16T19:31:01Z")

</div>

Thanks but i dont get it

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [June 17, 2019, 8:38am UTC](https://discourse.processing.org/t/save-int-for-android-program/12092/10 "2019-06-17T08:38:16Z")

</div>

@Nick1 ===the solution provided by @kfrajer has to work && mine too. Code snippet with sharedPrefrences:

```auto
import android.preference.PreferenceManager;
import android.content.Context;
import android.content.SharedPreferences;
/////////////////////////////////////////////////////////
int highscore= 0;
Context context;
public static final String MyPREFERENCES = "MyPrefs" ;
boolean presse = false;
SharedPreferences settings;

/////////////////////////////////////////////////////

 
void setup () {
  size(displayWidth, displayHeight);
  context= this.getActivity().getApplicationContext();
  settings = context.getSharedPreferences(MyPREFERENCES,0);//creer l'objet
  loadScore();//fonction qui charge le score
  println("lescore actuel est de====" + highscore);
}
 
void draw() {
  
  background(255,0,0);
    textSize(72);
  text(highscore, 100,100);
  
  
}

public void loadScore() {//get the score
  
    SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(this.getActivity().getApplicationContext());
    highscore = sharedPreferences.getInt("score", 0);
  }
  
  public void addScore(String key, int value){//save the score
    
    SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(this.getActivity().getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
  }
  
  
 
void mousePressed(){
highscore++;
addScore("score", highscore);//enregistrer le nouveau score

}
 
void onPause(){//save the score
  super.onPause();
  SharedPreferences settings =this.getActivity(). getSharedPreferences(MyPREFERENCES,0);
   SharedPreferences.Editor editor = settings.edit(); 
   editor.clear();
   editor.putInt("score", highscore); 
   editor.commit();
}
void onResume() {
super.onResume();
if(settings !=null){//au lancement settings est null....
    highscore = settings.getInt("score", 0); 
}
}

```
