# Saving instance state

**URL:** <https://discourse.processing.org/t/saving-instance-state/10535>\
**Category:** Processing for Android\
**Created:** [April 22, 2019, 6:38am UTC](https://discourse.processing.org/t/saving-instance-state/10535 "2019-04-22T06:38:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarrio](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sarrio](https://discourse.processing.org/u/Sarrio)\
**Post date:** [April 22, 2019, 6:38am UTC](https://discourse.processing.org/t/saving-instance-state/10535/1 "2019-04-22T06:38:25Z")

</div>

I’m writing a simple game using processing core library in android studio.  
I set up the project following the [Developing with Android Studio](https://android.processing.org/tutorials/android_studio/index.html) tutorial and everything works fine.

In particular in my project there is a main activity with a button which when is pressed launches a second activity (called **GameLauncher** ) that makes the sketch start.

The sketch is responsible for the core of the game, and of course when the user decides to close the game i need to save his progress.

I tried in the GameLauncher activity to implement the onSaveInstanceState() method, and get access to some data declared in the sketch file like so:

```auto
@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i("TEST: ", sketch.aPublicVariable);
    }

```

but android studio can’t resolve it.

- Is there a way to get access to the sketch data from outside, or to get notified inside the sketch of the app being stopped?

- Also, of course then the saved data needs to be retrieved and given to the sketch, or directly retrieved from inside the sketch, the next time the activity is called. How can i do that?

Thanks in advance.

Here’s the code of the GameLauncher class:

```auto
package com.foo.bar

import android.os.Bundle;
import android.content.Intent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.support.v7.app.AppCompatActivity;

import processing.android.PFragment;
import processing.android.CompatUtils;
import processing.core.PApplet;

public class GameLauncher extends AppCompatActivity {
    private PApplet sketch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout frame = new FrameLayout(this);
        frame.setId(CompatUtils.getUniqueViewId());
        setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        sketch = new ProcessingSketch();
        PFragment fragment = new PFragment(sketch);
        fragment.setView(frame, this);
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i("TEST: ", sketch.aPublicVariable);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        if (sketch != null) {
            sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        if (sketch != null) {
            sketch.onNewIntent(intent);
        }
    }
}

```

And here’s the code of the sketch:

```auto
package com.foo.bar;

import processing.core.PApplet;

public class ProcessingSketch extends PApplet {
    
public int aPublicVariable = 6;

public void settings(){
        fullScreen()
    }

    public void setup() {
        orientation(PORTRAIT);
    }

    public void draw() {
    }
}

```

---

<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:** [April 22, 2019, 8:30am UTC](https://discourse.processing.org/t/saving-instance-state/10535/2 "2019-04-22T08:30:58Z")

</div>

@Sarrio === onSaveInstanceSate has mainly be added for config changes (rotation of the phone) and you cannot use it for saving files, images downloaded and so on, though some kind of data can be added (int, String, with a String key as first argument) - Normally you do that at onPause() - Then you can recover these values by the same way using onRestoreInstanceState() writing : ```  
myPublicvariableData = savedInstanceState.getInt(“myKeyString”);  
Anyway it s difficult to answer not knowing what are the var you want to save; in many cases (scores, int, login …) the best way is to use Preferences methods for setting (onPause) or retrieving (onStart() or onCreate());

```auto

```

---

<div class="post-metadata">

**Author:** ![Sarrio](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sarrio](https://discourse.processing.org/u/Sarrio)\
**Post date:** [April 22, 2019, 8:42am UTC](https://discourse.processing.org/t/saving-instance-state/10535/3 "2019-04-22T08:42:13Z")

</div>

@akenaton thank for your answer.

I need to save some integer and boolean variables, plus a big boolean array (which i will probably save to a file).

Anyway my question is more about how can i do that from inside the sketch class:  
I can write code that saves and retrieves data from the outer activity class in the onPause() and onStart() methods, but I don’t know how to get the data I need to save because it belongs to the Sketch class

---

<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:** [April 22, 2019, 8:57am UTC](https://discourse.processing.org/t/saving-instance-state/10535/4 "2019-04-22T08:57:01Z")

</div>

@Sarrio ===  
in order to be sure taht i understand:  
a) you have some mainActivity with let us say a button: if the user clicks it it launches (intent) your GameLauncherActivity and here is created the framelayout for the sketch fragment. Is it right?  
b) if you want to save your vars in the sketch itself why not using save() (in onPause());  
c) if you want to save your sketch values in the outer activity could it be possible to declare them as static in your Sketch?

---

<div class="post-metadata">

**Author:** ![Sarrio](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sarrio](https://discourse.processing.org/u/Sarrio)\
**Post date:** [April 22, 2019, 9:01am UTC](https://discourse.processing.org/t/saving-instance-state/10535/5 "2019-04-22T09:01:39Z")

</div>

Dear @akenaton, Yes correct. Solved the problem using approach b). Thanks again
