How to correctly exit a Processing sketch in Android

edited September 2016 in Android Mode

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.

Tagged:

Answers

Sign In or Register to comment.