5

I have a Unity Scene built with Cardboard SDK and exported as a library for Android. The library is used to play videos in cardboard mode on the android app. it's not the whole app, but a part in it. The rest of the android app is built with Kotlin and Java.

I have implemented that and all the functions work as expected, but, exiting the scene crashes the android.

We tried various ways to clear player prefs and even clear memory before closing the scene. But on android it always crashes. I have two android phones with android 9 and 10 for testing.

In the android app, I have made it such that as soon as the app crashes, I try to recover. My crash is that some lateinit var variables are destroyed. Their value becomes null and recovering the previous activity crashes it. So right after I exit the unity scene, I load the dereferenced variables back into memory and everything works again.

Note: I have tried using Application.Quit(); in unity, but it just closes the whole app. On the other hand, I only want to close the running scene

In unity [I call a function goBack in android part to close the app]:

public void GoToHome()
    {
        Pause();
        Stop();
        Resources.UnloadUnusedAssets();
        PlayerPrefs.DeleteAll();
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo.Call("goBack");
    }

In App:

public void goBack()
    {
       UnityPlayer.currentActivity.finish();
       finish();
       loadDerereferencedVars();
    }

This goes perfectly on android 9. On the other phone with android 10, after I close the scene, the app continues to function, but, there comes a message

App keeps stopping

When I click close app, the app continues to work.

I have checked the logs and there is a null pointer dereference cause for the crash in Unity Main >...

If you'd like to see the Unity Crash Log from LogCat in Android Studio

So, since the app is still running, I thought, it would be better to just hide the crash report and just let the user not know about this crash, but still report it.

I tried enclosing my app in Application and added a method to catch uncaughtException.

here is my application class:

public class MyApp extends Application {

    private static final String TAG = "MyAPP";

    @Override
    public void onCreate() {
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(
                new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException (Thread thread, Throwable e) {
                        handleUncaughtException (thread, e);
                    }
                });
    }

    /**
     * Handles Uncaught Exceptions
     */
    private void handleUncaughtException (Thread thread, Throwable e) {

        // The following shows what I'd like, though it won't work like this.
        Toast.makeText(getApplicationContext(), "Looks like I am having a bad day!", Toast.LENGTH_LONG).show();
        Log.e("UncaughtException", "I found an exception!");
        // Add some code logic if needed based on your requirement
    }


}

Again, this works perfectly in Android 9 and I also got the error reported. However in the phone with android 10, I just get the crash report like the image above and no error is reported.

I want to know why the crash handling is not working and how can I fix it?

Hikki
  • 484
  • 1
  • 10
  • 21

1 Answers1

0

I would not finish the Activity you came from, instead just open a new intent (on UnityActivity). When you end this intent the app will come back to the last active Activity. I will give you my script as an example:

public void sendJobToUnity(String fileName, boolean isNewJob){
    //creates a new job. It exists inside the JobSelector Activity
    isUnityLoaded = true;

    //this is what you are looking for part1
    Intent i = new Intent(JobSelector.this, MainUnityActivity.class); //same as (CurrentActivity.this, UnityActivity.this)

    //those are how I send some data across the app. just ignore it
    //i.putExtra("jobName", fileName);
    //i.putExtra("isNewJob",isNewJob);
    //i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

    startActivityForResult(i, 1); //this is what you are looking for part2
}

For closing it, in the MainUnityActivity Activity I have an override that Unity sends to Android in order to Unload the activity (not quit it completely cause you cannot load it again if you do it) like this:

@Override
protected void receiveJobAndUnloadUnity(String data){
    saveCurrentJob(data); //saves the job it receives from Unity
    mUnityPlayer.unload(); //this is what you are looking for part3
}

If you want to unload Unity from android you can put "mUnityPlayer.unload();" wherever you want, provided you have started the Activity the way I've shown you. Note that "mUnityPlayer" is a default Unity variable and cannot be renamed