1

I'm working on a multiplayer game using libgdx. I want to handle sending some log out info to the server before the connection is closed. From what I've read Libgdx's ApplicationAdapter class' dispose() method is not always called when the app is closed, for instance if its closed while its paused. I've read other people suggest to others, who just want to dispose assets, that they should put it into the pause method, but in my situation I don't want it called when it's just paused, for instance if I just push the home screen or move to another app. I only want it to be called when I'm actually closing the app, such as if I were to go to the android application overview screen by pressing the bottom right button, to see all the apps running, and swiping up on my game to remove it and terminate it, or if the game crashes. Where can I put it so that it is called everytime the app terminates, but only if it terminates, not if its just paused or in the background. To reproduce I provided a generic libgdx project, all I added was the Log.i command.

package com.mycompany.mygame;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import android.util.*;

public class MyGdxGame implements ApplicationListener
{
    Texture texture;
    SpriteBatch batch;

    @Override
    public void create()
    {
        texture = new Texture(Gdx.files.internal("android.jpg"));
        batch = new SpriteBatch();
    }

    @Override
    public void render()
    {        
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(texture, Gdx.graphics.getWidth() / 4, 0, 
                   Gdx.graphics.getWidth() / 2, Gdx.graphics.getWidth() / 2);
        batch.end();
    }

    @Override
    public void dispose()
    {
        Log.i("test", "dispose ran");
        
    }

    @Override
    public void resize(int width, int height)
    {
    }

    @Override
    public void pause()
    {
    }

    @Override
    public void resume()
    {
    }
}

Michael
  • 33
  • 8
  • dispose what? You need to provide a [mcve] – OldProgrammer Mar 29 '23 at 21:53
  • The dispose method of libgdx's ApplicationAdapter class that it gets from the ApplicationListener interface. It's like the onDestroy method of java's Activity class, it get called whenever the Application is destroyed – Michael Mar 29 '23 at 21:58
  • To reproduce just create an empty libgdx project and override the dispose method in the generic ApplicationAdapter class that was created, then terminate it via Android's Application Overview screen, and if you use a debugger or a print or log command in the dispose method you'll see that the dispose method is never called – Michael Mar 29 '23 at 22:15
  • I added the sample code to reproduce – Michael Mar 29 '23 at 22:35
  • There is no possible way to do something that's guaranteed to happen every time the app is closed. Android can close your app suddenly without warning and no functions called. When a user swipes an app out of the task switcher, that is the behavior they should expect--to *immediately* halt the app from being able to do anything. If there is file resource clean-up to do, you can do it in `create()` so it happens the next time the app is opened. But there is no way to guarantee you can send some timely server message. – Tenfour04 Mar 30 '23 at 15:45
  • Well I was looking into it last night and it looks like I would be able to do it by using a service, or by using the ViewModel class' onCleared method, I would prefer to use the ViewModel class but it doesn't seem to be an option for me because the ViewModel library depends on some Kotlin libraries, and I'm using AIDE which doesn't support Kotlin, so I'm going to try and do it using a service instead. I wish I didn't have to use Aide, but I work all day every day, however I get a lot of time where I have nothing to do, but I can't go around carrying a laptop, and I don't have one anyway :/ – Michael Mar 30 '23 at 16:13
  • My other option is trying to see how ViewModel works and reproduce it using my own custom class' that mimic it but without using any Kotlin – Michael Mar 30 '23 at 16:19
  • Or see which class or classes in the ViewModel library are depending on Kotlin, and if I'm not using those classes, and they're not being used by the classes I'm using, then just removing them from the jar file to make a custom library – Michael Mar 30 '23 at 16:30
  • I don't know anything about AIDE, but I wouldn't expect it to matter to use ViewModel, since Kotlin is a transitive dependency. You wouldn't actually be working with any `.kt` source code. But it doesn't matter anyway. ViewModel.onCleared is not guaranteed to be called either when an app is destroyed by the OS. I'm really curious where you heard that a service can have an opportunity to run some code when your app is being destroyed by the OS, because I don't think that's true. – Tenfour04 Mar 31 '23 at 20:03
  • I looked into the classes in viewmodel and a lot of them are written mostly in kotlin, I had to go way back to much older versions, I think like 1.2, to find ones that weren't, but they indeed did not call onCleared, Idk if it never worked, or if it's because the older methods don't work on newer androids anymore, and the newer versions of viewmodel that use kotlin would work, idk. And as far as the services, apparently they used to be able to but arent anymore since Android started putting limitations on background services several years ago, so that's not an option either anymore – Michael Apr 01 '23 at 03:31
  • I'd be interested to know if the newer versions of viewmodel that use kotlin are able to, but doubt it – Michael Apr 01 '23 at 03:32
  • Especially going by what you say – Michael Apr 01 '23 at 03:32
  • And it was actually viewmodel 1.1.1 that I was able to get to work without kotlin – Michael Apr 01 '23 at 03:43
  • And here's a link where they were talking about services and viewmodel, there was another one I think but I can't find it: https://stackoverflow.com/questions/19568315/how-to-handle-running-service-when-app-is-killed-by-swiping-in-android – Michael Apr 01 '23 at 03:59
  • There's nothing magic about ViewModel. It's just part of a library and therefore can't change any of the rules or behavior set forth by the OS. There's no way to get the OS to always allow your app to run some more code when it's being forced closed. ViewModel cannot and could never achieve that. Kotlin is just a language that also compiles JVM code. It can't achieve anything that can't be done in Java. – Tenfour04 Apr 03 '23 at 02:56
  • I think I was mistaken however about what happens when a user swipes your app out of the task switcher. Apparently that does not terminate your application immediately, but only kills the task (activity stack), and your application is still alive in memory. So there is hope in that case that something can be done. Maybe ViewModel can get a callback in that case. But you definitely cannot handle the case where the user stops your app in the Android settings or when the OS kills your app to reclaim memory. – Tenfour04 Apr 03 '23 at 03:06
  • That's fine, I mostly wanted it for when the user swipe kills it – Michael Apr 03 '23 at 16:53

0 Answers0