7

I'm trying to implement Pausing in Unity by using OnApplicationPause function.

It seems to work fine when I exit my game (on Android) by pressing home button and then comeback through list of active apps, but when I press the game icon on home screen, it restart the game instead of bringing me back.

Is there any way around this ?

tshepang
  • 12,111
  • 21
  • 91
  • 136
tvoloshyn
  • 407
  • 8
  • 22

5 Answers5

7

for me this was happening when I had an: android.os.DeadObjectException in the logs. (Meaning the application already died).

Check to see if you have something like this in your logs:

I/ActivityManager(  600): Restarting because process died: ActivityRecord{439f9588 u0 com.mycompany.myapp/com.unity3d.player.UnityPlayerProxyActivity}
W/ActivityManager(  600): Exception when starting activity com.mycompany.myapp/com.unity3d.player.UnityPlayerProxyActivity
W/ActivityManager(  600): android.os.DeadObjectException
W/ActivityManager(  600):   at android.os.BinderProxy.transact(Native Method)
W/ActivityManager(  600):   at android.app.ApplicationThreadProxy.scheduleLaunchActivity(ApplicationThreadNative.java:759)
W/ActivityManager(  600):   at com.android.server.am.ActivityStack.realStartActivityLocked(ActivityStack.java:1120)
W/ActivityManager(  600):   at com.android.server.am.ActivityStack.startSpecificActivityLocked(ActivityStack.java:1247)

...and if so look back even further in your logs to see why you app is dying. For me to keep the application from dying I had to move the intent filters:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

from UnityPlayerProxyActivity to UnityPlayerNativeActivity

Stanley
  • 1,421
  • 5
  • 19
  • 36
  • Moving this intent-filter worked for me too! I have no idea why, but that is besides the point! – p.streef Jul 11 '14 at 16:41
  • Beware - this worked for me, but moving the intent filter had some bad effects. Existing users of my game lost the icon if they had pinned it to their home page, and sometimes (I'm not sure of the circumstances) the icon would disappear altogether from the launcher. – tenpn Aug 14 '15 at 14:46
0

It should be resuming on the point you go back to the game. If not, then it may be your game flow automatically restart when it got back from pausing.

In your game, try to check the state of OnApplicationPause and debug from there. The code sample is in this thread http://answers.unity3d.com/questions/286939/detecting-when-applicationdidbecomeactive-in-unity.html

  • Could that be because I have 2 scenes in my project ? – tvoloshyn Feb 19 '14 at 11:52
  • nope, that should not be a problem. One possibility is that your application got an error when resuming, and that's why it's restarted. You might want to check on the logcat like in @Enzign answer. – Yuandra Ismiraldi Feb 20 '14 at 14:42
0

In my experience these strange things happens when you don't have all the right information in your AndroidManifest.xml file. Specifically this line:

<activity android:name="your_name_here.Activity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="your_orientation_here_if_needed">

Make sure the "android:configChanges" contains everything in here. Otherwise to give you more specific information you would need to provide a logcat of when this happens.

Enzign
  • 43
  • 5
0

have you check the launch mode on android manifest and make the value like this android:launchMode="singleTask"

juanc
  • 1
0

Did you have a try manipulating the life cycle of your app when you export it as Android gradle project?

You can do so (in Unity 2018.4.5, other versions may vary) by ticking Export Project in your File - Build Settings and clicking Export. You can open the resulting gradle project for example in Android Studio and should find a UnityPlayerActivity with the following situation:

    // Pause 
    @Override protected void onPause()
    {
        super.onPause();
        mUnityPlayer.pause();
    }

    // Resume Unity
    @Override protected void onResume()
    {
        super.onResume();
        mUnityPlayer.resume();
    }

    @Override protected void onStart()
    {
        super.onStart();
        mUnityPlayer.start();
    }

    @Override protected void onStop()
    {
        super.onStop();
        mUnityPlayer.stop();
    }

    // Low Memory Unity
    @Override public void onLowMemory()
    {
        super.onLowMemory();
        mUnityPlayer.lowMemory();
    }

    // Trim Memory Unity
    @Override public void onTrimMemory(int level)
    {
        super.onTrimMemory(level);
        if (level == TRIM_MEMORY_RUNNING_CRITICAL)
        {
            mUnityPlayer.lowMemory();
        }
    }

I have no experience with this, but wanted to show you the possibility :)

Benjamin Zach
  • 1,452
  • 2
  • 18
  • 38