Search code examples
libgdxresumerobovmpause

Elements disappear after pause and resume


Testing my libGDX app in RoboVM, I have encountered a major problem. When I pause my app (by actually going to the Home screen or sending app invites via Facebook) and then return to my app, classes of my games disappear. As if it does not store data properly on the resume() method. First i though it there was a problem of my AssetLoader, but after some debugging I found that the situation is worse. Actual instances of classes and shapes disappear. As if they never existed.

After googling the issue, I found that it might be related to IOSGraphics, but I have not managed to fix the problem.

My IOSLauncher looks something like this, I have erased the Facebook & Google AdMob specific code.

 protected IOSApplication createApplication() {

        IOSApplicationConfiguration config = new IOSApplicationConfiguration();
        config.useAccelerometer = true;
        config.useCompass = true;
        config.orientationPortrait = true;
        config.orientationLandscape = false;
        return new IOSApplication(new Game(this), config);
    }

    @Override
    public boolean didFinishLaunching(UIApplication application,
                                      UIApplicationLaunchOptions launchOptions) {
        FBSDKApplicationDelegate.getSharedInstance().didFinishLaunching(application, launchOptions);
        initialize();
        return true;
    }


    public void initialize() {
        //...
    }


    public static void main(String[] argv) {
        NSAutoreleasePool pool = new NSAutoreleasePool();
        UIApplication.main(argv, null, IOSLauncher.class);
        pool.close();
    }

    @Override
    public void showAds(boolean show) {
        //...
    }

    @Override
    public void shareOnFacebook() {
        //...
    }

    @Override
    public void inviteFriends() {
        //....
    }

    @Override
    public boolean openURL(UIApplication application, NSURL url,
                           String sourceApplication, NSPropertyList annotation) {
        super.openURL(application, url, sourceApplication, annotation);
        return FBSDKApplicationDelegate.getSharedInstance().openURL(
                application, url, sourceApplication, annotation);
    }

    @Override
    public void didBecomeActive(UIApplication application) {
        super.didBecomeActive(application);
        FBSDKAppEvents.activateApp();
    }

    @Override
    public void willResignActive(UIApplication application) {
        super.willResignActive(application);
    }

    @Override
    public void willTerminate(UIApplication application) {
        super.willTerminate(application);
    }
}

Solution

  • After a couple of days filled with headache I found the solution!

    LifeCycle methods like pause & resume, hide & show are not always called When they are supposed to be called, therefore data is not stored properly. This issue can completely break your game.

    This thing only occurred when testing my game on the iOS platform, mainly when I opened a 3rd party app, Facebook in this case. No such thing found on Android, though.

    The only thing I changed on the iOS version was calling the mentioned methods manually to make sure it always pauses and resumes when it has to.