We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everybody!
It seems like setup()
is called every time the device is turned and the orientation is switched. I want the app to be displayed in both orientations, so orientation lock would not make sense.
How can we prevent restarting the app when switching orientation and instead eg. only recalculate positions / sizes?
Here is the little code-snippet I wrote to test, if setup is called / app is restarted:
color c;
void setup() {
c = color(0);
}
void draw() {
background(c);
}
void mousePressed() {
c = color (255);
}
I also tried boolean isLaunched = false;
to test if only setup()
is called or if the app is restarted completely. Since the boolean does not affect the behaviour, i suppose, the app is restarted.
Thanks in advance for helping out!
Answers
In most cases, allowing the sketch to maintain its previous state would create more problems than it would solve... so the current implementation is probably for the best. However, if you want to be able to adapt to screen rotations, you should be able to do so in a similar way to how it is done in native Android apps. The following approach is one way to do it, assuming that it works in Processing:
In the
<activity>
tag, insert the following attribute. This will prevent theActivity
from restarting when the device is rotated:In your sketch, import the following native Android class:
In your sketch, insert the following native method:
To properly configure your entire sketch for the orientation change, you will need to change variables such as
width
andheight
... and that may not even be allowed by Processing, so you may need to create custom variables that are set to these values initially and are changed upon screen rotation.Also, you may need to re-adjust the display to draw sideways (and which way sideways, you'll have to find out) in accordance with the orientation if this isn't done for you. You can probably accomplish this using some matrix transformations at the start of
draw()
.This seems complicated, and rightly so. Native Android developers don't have to deal with this kind of mess (most of the time) because their applications are designed to be restarted in
onCreate()
. However, Processing's framework wasn't built around this. It is still possible to work into this model, but this would require saving all of the application's current data into temporary memory... and that is a hassle. If you think your application would be better suited for this approach, then you can try... but I think that the method outlined above is more appropriate for almost any Processing application.