OpenGL and apps in background in iOS 5.1

I’ve stumbled upon this issue here: . As far as I can see, there’s no way to currently react to the user pressing the home button to suspend/resume a JUCE application. This has become necessary when using OpenGL for rendering, as all rendering has to be stopped when the app is suspended, as described in the link. I think what should be done is to implement the applicationDidEnterBackground and applicationWillEnterForeground methods in , and then to add similar functions to the JUCEApplication class where one could start or stop OpenGL playback (unless I’ve overlooked another, already existing possibility).

FWIW, I added a suspend and resume to the juce base app class. Then wired it up for iOS and Android. I needed to close and restore some network connections. I did not make the pure virtual, just virtual, so the changes wouldn’t break any other juce apps.

I’m sure Jules will get to suspend/resume eventually, but, in the mean time, the changes are pretty minor.

Yeah, sorry chaps, I’ve just not got around to this yet. If anyone wants to post some suggested code that works on iOS and android, that might kick me into action :wink:

Well, I haven’t researched the Android part yet, but what works on iOS is the following: add the applicationDidEnterBackground and applicationWillEnterForeground methods in the startup delegate in like this:

@interface JuceAppStartupDelegate : NSObject

  • (void) applicationDidFinishLaunching: (UIApplication*) application;
  • (void) applicationWillTerminate: (UIApplication*) application;
  • (void)applicationDidEnterBackground:(UIApplication *)application;
  • (void)applicationWillEnterForeground:(UIApplication *)application;


@implementation JuceAppStartupDelegate

  • (void) applicationDidFinishLaunching: (UIApplication*) application
    JUCEApplication* app = dynamic_cast <JUCEApplication*> (JUCEApplicationBase::createInstance());
    if (! app->initialiseApp (String::empty))
    exit (0);

  • (void) applicationWillTerminate: (UIApplication*) application

  • (void)applicationDidEnterBackground:(UIApplication )application
    app = JUCEApplication::getInstance();

  • (void)applicationWillEnterForeground:(UIApplication )application
    app = JUCEApplicationBase::getInstance();

Then, in juce_Application.h you need to add the suspend and resume methods by

virtual void suspend(){};
virtual void resume(){};

As jfitzpat said, they should be virtual to ensure compatibility. Now in your actual application, add whatever needs to be done in suspend() and resume() (like, in my case, attaching and detaching the OpenGL context).

Sorry, I missed the request yesterday. Here is juce_android_Windowing.cpp, juce_ApplicationBase.cpp, juce_ApplicationBase.h,, and I just merged with today’s tip. Suspend and Resume should be the only changes.

** Note: I didn’t grab all four iOS delegates like above. I intended to just generalize it as suspend/resume across all five platforms, but never got around to adding the desktops.

Cool stuff, thanks chaps! I’ll check something in shortly…