0

I am currently building an app in Android Studio involving a Processing sketch.

Main class:

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;

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

public class ClassMain extends Activity {
    /**************************************************/
    PApplet sketch;
    /**************************************************/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class_main);

        FragmentManager fragmentManager = getFragmentManager();
        sketch = new ClassSketch();
        PFragment fragment = new PFragment();
        fragment.setSketch(sketch);
        fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
    }
    /**************************************************/
    @Override
    public void onBackPressed()
    {

        //ClassMain.this.finish();
    }
    /**************************************************/
    //It doesn't work if called from sketch...
    public void exits()
    {
        ClassMain.this.finish();
    }
    /**************************************************/
}

Processing Sketch:

import android.view.KeyEvent;
import processing.core.PApplet;

public class ClassSketch extends PApplet {
    /*****************************************************************************/
    public void settings()
    {
        size(displayWidth, displayHeight);
    }
    /*****************************************************************************/
    public void setup()
    {
    }
    /*****************************************************************************/
    public void draw()
    {
        keykey();
    }
    public void keykey()
    {
        if(keyPressed)
        {
            if (key == CODED) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    //non of the following work:
                    //exit();
                    ClassMain j = new ClassMain();
                    j.exits();
                }
            }
        }
    }
}

Here is the problem, I want the the user, depending on some circumstances that I will add later in the sketch, to exit not only the sketch but the class that is hosting it (ClassMain). When ever (in the sketch) something happens in my sketch, I want to be able to exit the whole MainClass, but I just happen to fail. Thanks for the support.

Zardoz
  • 79
  • 1
  • 2
  • 11
  • Please link between crossposts: https://forum.processing.org/two/discussion/18230/how-to-correctly-exit-a-processing-sketch-in-android – Kevin Workman Sep 21 '16 at 15:44

2 Answers2

0

This doesn't make sense:

ClassMain j = new ClassMain();
j.exits();

Here you're creating a new instance of ClassMain, and then immediately exiting. That won't do anything.

Instead, you need to tell the already-existing parent ClassMain to exit.

To do this, you need a reference to the parent ClassMain instance, which you can take into your ClassSketch constructor:

public class ClassSketch extends PApplet {

    private ClassMain parent;

    public ClassSketch(ClassMain parent){
       this.parent = parent;
    }

    public void keykey(){
       parent.exits();
    }
}

Then to pass the instance into the constructor, you can simply use the this keyword:

protected void onCreate(Bundle savedInstanceState) {
        sketch = new ClassSketch(this);
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
0

As a complementary note to the above, don't forget to call super.onCreate(savedInstanceState); in your onCreate() function.

You can exit directly from your sketch by accessing finish() via: this.getActivity().finish();. Notice that in API level 16 and above, you also have available this.getActivity().finishAffinity(); as commented in here.

Lastly, if you press the home button, it calls onStop(), so the app lingers in Memory. If you press the back button, then onDestroy() gets called as well. If you call finish() as described, it is guaranteed onDestroy() gets called.

My two cents...

Kf

K F
  • 1,368
  • 13
  • 20