Sketch resets on rotating

on Android, every time I rotate the screen, the sketch resets / restarts. Is there a way to disable this or should I fix the orientation and use gyroscope to detect rotation?

If this is duplicate, could you send me a link to the other post?

1 Like

Please Post some code, so we can See where the Problem lies.

that s absolutely normal!
either you fix the orientation (portrait or landscape) in the manifest (you can also do that by code) or you choose to handle yourself what is supposed to happens onConfigurationChanged().

@Lexyth, well, it is any app. Just draw text displaying frameCount and see for yourself.

@akenaton, I’ll take a look at onConfigurationChanged(). If it is too complicated, I’ll just use gyroscope.

I have a workaround but without code i can’t do any more than explain stuff. Try saving your variables to files every second then when your sketch reloads load those files.

This is normal. You can read what happens in the life cycle here

However there are some things going on in Processing for Android that are bugs, I think.
In the code below I use empty life cycle functions but I am not allowed to use onPause(), because if I do and press the home button, and call the app back, no text will appear but only a blue background. If I use the back button it will.
Also it is a good idea to use the orientation function in onCreate(), otherwise the app often crashes.
Another thing is that you need to use super.on Resume in onResume() otherwise the app turns black or crashes.

import android.os.Bundle;

void setup() {
  text("Hello world", width/2, height/2);

public void settings() {  

void draw() {

void onCreate(Bundle savedInstanceState) {

void onStart() {

// void onPause() {
// }

void onStop() {

void onResume() {
  super.onResume();  //  Is necessary because otherwise screen will turn black

void onRestart() {

The onPause() bug is now marked as core bug on github.

I could share you guys some ideas, maybe far too complex for beginners.

Try to serialise your drawings in the override method onStop(), for example you can use google Gson library(a library serialise object to string) with android sharedPreference.

In setup method, you can initial these drawings from sharedPreference.

If you want to rotate without lose your drawings, for example you design a drawing app for large screen tablet, this will be the a easy solution.

You are right. But I think most of Processing users are makers, artists , or learning to code. I think that was what Processing was made for. Android is a very complex structure, and personally I want to keep it as simple as possible just with some widgets. I’ve looked into Gson here but I don’t understand how you would like to set it up. Maybe you could post a small example about how to solve the rotation problem. That would be great!

It is not necessarily to use Gson. I said for example.
A simple situation could be, you have a list of objects, these objects contains x,y co-ordinates. In its own draw method it has somethings like circle(x,y,radius).
When you rotate the screen, these objects disappear.
Then you need some techniques to serialise these objects(e.g. Gson) and re-initialise them in setup and call its own draw method.
Then you need understand the lifecycle of android, that’s why I say put serialisation in onStop().

As an old saying goes “Give somebody a fish and you feed them for a day. Teach somebody to fish and you feed them for a lifetime.”
There’s no straight solution for these questions, it all depends on your situation. So I just share the idea here.

1 Like

OK I got it. You are talking abaut the subject matter the topic started with. Good idea! But it can’t solve the onPause () problem I had in mind.

// alright I made a sketch that it is IMPOSSIBLE
// TO RESET!! If you want it to reset you must
// use something with the time.
// also it will only work in app mode not sketch
// mode. sorry if I’m too descriptive but I
// just got out of a class that had beginners in it

// Hello this is a small app I made
// that will not reset when rotated
// but it MUST BE RUN AS AN APP!

// Our first line here makes a
// variable called orientation.
// you must have this line.
// right now it does nothing.
// it stands for a word but we havent
// decided which word yet.
String orientation;

// our second line here creates a
// file name. Im not sure how it 
// works because i found it on another 
// forum. but it is required
File pfile;

// these 2 lines make a variable.
// for example if i type "x" the 
// program thinks i typed 0.
// these are the location of the 
// circle.
float x = 0;
float y = 0;

// these next 2 lines are true or false
// variables. for example if the 
// command is if(touching) then it
// will not run because it is false
// not true. if the command was 
// if(!(touching)) then it WOULD run.
// the ! means not.
boolean DONT_DELETE = false;
boolean touching = false;

// these lines of code create MULTIPLE
// variables. how many? not decided
// yet. we will tell it later
String[] save;
float[] saven;

// this creates a set of code that will
// only run ONE TIME! such as setting
// positions
void setup(){

// this is where the hard stuff comes
// in. remember the variable we set 
// earlier that stood for a word?
// well now well say WHAT word

// this line says if the width of
// the screen is greater then the
// height the phone must be sideways
// so make the word "landscape"
if(width > height){
orientation = "landscape";

// this line is the opposite. if the
// height is greater then the width 
// the phone must be up
// set the word to "portrait"
if(height > width){
orientation = "portrait";

// if the file save.txt does NOT
// exist (meaning it is the first
// time running the program) then
// this will run.

// remember earlier we made one word
// stand for multiple variables?
// well this says how many.
// in this case we make it stand 
// for 2 variables: saven[0] and 
// saven[1]
saven = new float [2];

// this is the same thing except
// instead of numbers it is words.
// this stands for 3 words but all
// stand for "". the variables are
// save[0], save[1], and save[2].
save = new String[3];

// now for what makes this all work.
// if the file save.txt exists then
// do whats below.

// remember anything from here to
// the next } will only run if
// the file save.txt exists. 
// normally we would name the file
// save.sav but this is for learning
// so its save.txt. the reason for
// the .sav is so other people wont
// mess with it. this at first will
// not run because save.txt does not
// exist. we will make it later.

// remember earlier when we made 
// multiple variables connected to
// one word? well this actually
// assigns words or strings to it.
// this will load the strings from
// the file save.txt
save = loadStrings("save.txt");

// this makes saven stand for
// the number of strings in save.
// if save is 3 lines long then
// this stands for 3 numbers
// all of which are 0.
saven = new float[save.length];

// if the program ran before, it saved
// the orientation to save[2].
// now if the orientation is
// still the same as last time
// then the variables saven[0] and
// saven[1] equal save[0] and save[1].

// this makes it impossible to reset
// if you want it to reset when closed
// simply delete this line.
if(orientation == save[2]){

// but save[0] is a word not a number.
// in order to turn it into
// a number we must use the float()
// command. except it only turns
// the word 1 into the number 1
// it cannot turn the word one
// into the number 1.
saven[0] = float(save[0]);
saven[1] = float(save[1]);

// this says if the orientation was
// flipped or is not the same
// as last time then flip x
// and y (or saven[1] and saven[0].
if(!(orientation == save[2])){
saven[1] = float(save[0]);
saven[0] = float(save[1]);


// after all the above code this makes 
// x saven[0] and y saven[1].
x = saven[0];
y = saven[1];

// here we will make the 3rd variable
// save[2] equal to whatever the 
// orientation of the screen is.
// we do this at the end so that if
// we already ran the program before it
// will compare what the orientation
// was and what it is.
// if we were to run this at the 
// beggining then it would always be the
// same as orientation.
save[2] = orientation;

void draw(){
// this basically draws a big rectangle
// over the whole screen. its a white
// rectangle

// everything below this will be black

// if the screen is touched
// then saven[0] and saven[1] equal
// mouse x and y
saven[0] = mouseX;
saven[1] = mouseY;

// x equals saven[0]
// y equals saven[1]
x = saven[0];
y = saven[1];

// whatever x and y are put a circle
// there

// time to save it. if we save it then 
// it CANNOT reset. but variables
// cannot be saved. only numbers
// can. so save[0] equals
// str(saven[0]) and same for
// save[1]. anything can be turned
// into a word but a word cannot
// be turned into just anything
save[0] = str(saven[0]);
save[1] = str(saven[1]);

// finally save the strings save to 
// save.txt

// this says if screen touched
void touchStarted(){
// then touching equals true. because
// if you touched the screen then 
// you are touching it
touching = true;

// if you release your finger off
// the screen then
void touchEnded(){
// touching equals false because
// you arent touching it anymore
touching = false;

// um.... I dont know what this does
// i got it from another
// forum. it checks if a file
// exists. feel free to use it.
boolean fileExists(String qaz){
pfile = new File(sketchPath(qaz));
DONT_DELETE = false;

// I hope it works for you! :grinning: