Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to exit UnityPlayerActivity not to kill whole process?

Discussion in 'Android' started by alesrazym, Aug 23, 2018.

  1. alesrazym

    alesrazym

    Joined:
    May 11, 2018
    Posts:
    17
    Dear Unity developers,
    I'm trying to implement Unity (UnityPlayerActivity) not as a single activity but as one of several activities within an application.

    What I want is to exit from UnityPlayerActivity/UnityPlayer not to kill whole process and be able to run UnityPlayerActivity again.

    I tried to:
    • override UnityPlayer.kill(), where Process.killProcess(Process.myPid()) resides with empty implementation, or
    • not to call UnityPlayer.pause(), UnityPlayer.quit() methods, or
    • not to call some parts of UnityPlayer.quit() method (via reflexion).
    It always ended up with:
    • process termination (silent, with no exception), or
    • process termination (with native exception), or
    • unity running and leaked in memory (with context etc. probably) and process termination on start of second run of UnityPlayerActivity (with native exception).
    Reason I don't want to start UnityPlayerActivity in a separate process and kill it is binding to local service.

    Does anyone knows how to solve this issue, or killing the process (and communicate with service with AIDL or Broadcasts or ...?) is the only possibility?

    Is it a bug?

    Thanks for replies
    Ales
     
  2. TheHighGround

    TheHighGround

    Joined:
    Nov 19, 2017
    Posts:
    68
    I have no idea what you're trying to do...Android uses a Stack for it's activities. If you want to run an other activity it gets placed onto the stack. Hence why when you hit the back button it just goes to the previous activity in the stack like a normal stack data structure works.

    If you want to have an activity that launches the Unity one, all you have to do is override the AndroidManifest.xml and set your custom Activity as the MainLauncher one. From your custom activity you can just do

    Code (CSharp):
    1.  
    2. Intent intent = new Intent(this, UnityPlayerActivity.class);
    3. this.startActivity(intent);
    4.  
    Maybe I'm not understanding your question but this is what I got out of it.
     
  3. alesrazym

    alesrazym

    Joined:
    May 11, 2018
    Posts:
    17
    Thanks for reply,
    I do it this way, but then, when I want to finish UnityPlayerActivity, it crashes in native libs or kill whole app process silently.

    If I do it like suggested - call Activity.finish(), call UnityPlayer.pause() in Activity.onPause(), call UnityPlayer.quit() in Activity.onDestroy(), native exception is thrown:

    Code (CSharp):
    1. Build fingerprint: 'samsung/gt510wifixx/gt510wifi:7.1.1/NMF26X/T550XXU1CQJ6:user/release-keys'
    2. pid: 18275, tid: 18728, name: UnityMain  >>> myapp <<<
    3. Stack frame #00  pc 00f87924  /data/app/myapp-1/lib/arm/libunity.so: Routine TestAndRemoveObject(Object*, Unity::Type const*, dynamic_array<Object*, 0u>&) at ??:?
    4. Stack frame #01  pc 00f87528  /data/app/myapp-1/lib/arm/libunity.so: Routine MemoryProfilerStats::UnregisterObject(Object*) at ??:?
    5. Stack frame #02  pc 00735848  /data/app/myapp-1/lib/arm/libunity.so: Routine delete_object_internal_step1(Object*) at ??:?
    6. Stack frame #03  pc 007352a0  /data/app/myapp-1/lib/arm/libunity.so: Routine DestroySingleObject(Object*) at ??:?
    7. Stack frame #04  pc 005f26f4  /data/app/myapp-1/lib/arm/libunity.so: Routine AndroidVideoMedia<AndroidMediaNDK::Traits>::VideoDecoder::Destroy() at ??:?
    8. Stack frame #05  pc 005f5f3c  /data/app/myapp-1/lib/arm/libunity.so: Routine AndroidVideoMedia<AndroidMediaNDK::Traits>::Close(AndroidMediaNDK::Env&) at ??:?
    9. Stack frame #06  pc 005efe08  /data/app/myapp-1/lib/arm/libunity.so: Routine AndroidVideoMedia<AndroidMediaNDK::Traits>::Close() at ??:?
    10. Stack frame #07  pc 005ee938  /data/app/myapp-1/lib/arm/libunity.so: Routine AndroidVideoMedia<AndroidMediaNDK::Traits>::~AndroidVideoMedia() at ??:?
    11. Stack frame #08  pc 005c8484  /data/app/myapp-1/lib/arm/libunity.so: Routine VideoClipPlayback::~VideoClipPlayback() at ??:?
    12. Stack frame #09  pc 005c7c00  /data/app/myapp-1/lib/arm/libunity.so: Routine VideoPlaybackMgr::DestructionMgr::DestroyAllPossible() at ??:?
    13. Stack frame #10  pc 005c6e44  /data/app/myapp-1/lib/arm/libunity.so: Routine VideoPlaybackMgr::~VideoPlaybackMgr()::PlaybackDestructionLoop::Execute(VideoPlaybackMgr::DestructionMgr*) at ??:?
    14. Stack frame #11  pc 005c6da8  /data/app/myapp-1/lib/arm/libunity.so: Routine VideoPlaybackMgr::~VideoPlaybackMgr() at ??:?
    15. Stack frame #12  pc 005ba1b8  /data/app/myapp-1/lib/arm/libunity.so: Routine MediaAPI::Playback::Shutdown() at ??:?
    16. Stack frame #13  pc 00748f30  /data/app/myapp-1/lib/arm/libunity.so: Routine TypeManager::CleanupAllTypes() at ??:?
    17. Stack frame #14  pc 00d98fb4  /data/app/myapp-1/lib/arm/libunity.so: Routine CleanupEngine() at ??:?
    18. Stack frame #15  pc 00d98ee0  /data/app/myapp-1/lib/arm/libunity.so: Routine PlayerCleanup(bool) at ??:?
    19. Stack frame #16  pc 0061bba0  /data/app/myapp-1/lib/arm/libunity.so: Routine UnityDeinitApplication() at ??:?
    20. Stack frame #17  pc 0061ec34  /data/app/myapp-1/lib/arm/libunity.so: Routine nativeDone(_JNIEnv*, _jobject*) at ??:?
    21. Stack frame #18  pc 000a3499  /system/lib/libart.so
    22. Stack frame #19  pc 000a3499  /system/lib/libart.so
    23. Stack frame #20  pc 000a3499  /system/lib/libart.so
    24. Stack frame #21  pc 000a3499  /system/lib/libart.so
    25. Stack frame #22  pc 000a3499  /system/lib/libart.so
    26. Stack frame #23  pc 000a3499  /system/lib/libart.so
    27. Stack frame #24  pc 000a3499  /system/lib/libart.so
    28. Stack frame #25  pc 000a3499  /system/lib/libart.so
    29. Stack frame #26  pc 000a3499  /system/lib/libart.so
    30. Stack frame #27  pc 000a3499  /system/lib/libart.so
    31. Stack frame #28  pc 000a3499  /system/lib/libart.so
    32. Stack frame #29  pc 000a3499  /system/lib/libart.so
    33. Stack frame #30  pc 000a3499  /system/lib/libart.so
    34. Stack frame #31  pc 000a3499  /system/lib/libart.so
    35. Crash dump is completed
    36.  
    But it crashes with different signals on different threads sometimes:

    Code (CSharp):
    1. signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000
    2. Build fingerprint: 'samsung/gt510wifixx/gt510wifi:7.1.1/NMF26X/T550XXU1CQJ6:user/release-keys'
    3. pid: 19510, tid: 20087, name: UnityChoreograp  >>> myapp <<<
    4.  
    or

    Code (CSharp):
    1. signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 6ec24948
    2. Build fingerprint: 'samsung/gt510wifixx/gt510wifi:7.1.1/NMF26X/T550XXU1CQJ6:user/release-keys'
    3. pid: 20323, tid: 20828, name: UnityMain  >>> myapp <<<
    Currently its build with Mono scripting backend in Unity 2018.2.0f2 and 2018.2.4f1 (both behave the same)
     
  4. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    218
    Seems like Unity have a bug when the activity gets destroyed as stated in

    https://forum.unity.com/threads/crash-sessions-increase-to-4-with-5-4-android-build.428327/

    They are having problems with the repro of the bug. If you are able to reproduce it , you can send a bug report.

    However, another way will be to use an android plugin into the unity projects that call android native code.

    As a suggestion I would try to open a new activity and then clear the android activity stack as a workaround
     
  5. alesrazym

    alesrazym

    Joined:
    May 11, 2018
    Posts:
    17
    Thanks for reply and link, it seems to be a different issue to me.
     
  6. WiesenWang

    WiesenWang

    Joined:
    Nov 22, 2018
    Posts:
    3
    hi,Have you solved this issue,I have the same problem
     
  7. alesrazym

    alesrazym

    Joined:
    May 11, 2018
    Posts:
    17
    Hi, we solved this by running unity in separate process (specified in AndroidManifest) and communicate (Android native app service <---> UnityActivity) via sockets. Then simply don't care about crashes when finishing. Still think it's not best (clear) solution, but working one.

    BR and marry xmas :)
     
  8. WiesenWang

    WiesenWang

    Joined:
    Nov 22, 2018
    Posts:
    3
    yeah! i solved with the same method,but it will create much more proess issue, after all, the unity game just a part of my program!so i want to find better method!

    Merry Christmas!!:p
     
  9. ashish_unity707

    ashish_unity707

    Joined:
    Jun 28, 2021
    Posts:
    1
    Have you found any permanent solution to it?
     
  10. RJSD3V

    RJSD3V

    Joined:
    Aug 30, 2019
    Posts:
    4
    I am facing the same problem, any stable solutions to it?
     
  11. alesrazym

    alesrazym

    Joined:
    May 11, 2018
    Posts:
    17