Saving instance state

#1

I’m writing a simple game using processing core library in android studio.
I set up the project following the Developing with Android Studio 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:

@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:

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:

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() {
    }
}
#2

@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());

#3

@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

#4

@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?

#5

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