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