31

(I see a similar question on stackoverflow, but the answer there is not a true answer, and the context of the problem is a bit different too.)

"java.lang.RuntimeException: Performing pause of activity that is not resumed"

I develop a game application (which uses both normal Views and GLSurfaceView). If I turn on and off my phone display very fast, I can cause this exception sometimes (thrown by ActivityThread ), but my application is running normally after the exception. My app is a landscape one, and this is correctly set in the manifest as well (including orientation and configchanges as well).

Is this OK?

It's a RuntimeException thrown by ActivityThread under the application name of my application, but it doesn't terminate my app.

Thomas Calc
  • 2,994
  • 3
  • 30
  • 56
  • It generally helps if you post some code so people can have a look at what's wrong. I take it that turning on your phone somehow messes up the pause and resumes in Android. I wouldn't worry about it too much, unless your game involves players turning on and off their phones very fast. – Christine Jun 01 '12 at 04:05
  • I know some people make the mistake of calling onPause or onResume explicitly, but I don't. The application is too huge and complex to post any part of it; of course, I could use pseudo-code but not sure from where I should take it, as the problem is general. Hopefully it's harmless. – Thomas Calc Jun 01 '12 at 04:24
  • Ah, calling onPause or onResume is not a mistake. It's a good place to put code in. In your case, I would put Log statements in all onResume and onPause methods in all activities, and inspect the log files after the exception occurs. At least then you know what onPause is causing the problem. – Christine Jun 01 '12 at 04:30
  • Yes, but I mentioned calling Activity.onPause and onResume() **explicitly**, that is a mistake. The system calls onPause() and onResume() automatically, you should never call them from your own code. About logging, thanks, I'm trying to log whatever I can. The problem occurs only in robustness tests (screen on/off etc.). – Thomas Calc Jun 01 '12 at 12:57
  • Yes, don't call the onPause(), you're right. – Christine Jun 04 '12 at 01:06

2 Answers2

55

I've just had this problem because I was calling activity.recreate() (or .finish() and .startActivity() - depending on android version). Of course, you would only call those functions because you want to reload language, reset orientation and similar stuff that you can only do with activity recreation.

You can't call those functions (.finish() or .recreate()) from onResume() though. If you do, you will receive the mentioned non-fatal exception.

I "solved" the issue by delaying the .recreate() call for one millisecond so that the activity gets properly resumed and is only then killed.

  Handler handler = new Handler();
  handler.postDelayed(new Runnable()
  {
    @Override
    public void run()
    {
      log.i("TX.refresh", "Switching to %s from %s", lang, lang);
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
      {
        ctx.finish();
        ctx.startActivity(ctx.getIntent());
      } else ctx.recreate();
    }
  }, 1);
velis
  • 8,747
  • 4
  • 44
  • 64
  • thank you so much for this, i had mentioned exceptions while changing lang and getting back to activity. this solved my exceptions. thx...still dont know why just delaying 1ms solved it... – To Kra Apr 06 '15 at 17:08
  • 1
    The problem is that within OnResume, your activity is not yet resumed. Therefore you get the error message. Delaying by 1 ms lets the framework do its magic and only then recreates the activity. – velis Apr 07 '15 at 09:19
  • 6
    Btw a '0' timeout will work as well. '0' means "put on end of execution queue" which is exactly what we want in this case:) Thx! – guy_m Aug 26 '15 at 10:00
  • @velis the line `Handler handler = new Handler();` does not work for me as `Handler` is an abstract class. EDIT: I spoke too soon. Android studio had automatically imported the wrong handler for me. @velis was correct. If someone happens to run into a similar problem be sure to import `android.os.Handler` – Awalrod Jun 30 '16 at 15:23
  • I was also trying to call recreate from an activity's onResume method and I ended up seeing that my activity's onPause method was called followed by onResume followed by another onPause. Since the onPause was called after onResume my activity was not left in a resumed state. I ended up solving this problem using the same postDelayed solution. – John Weidner Jun 27 '19 at 16:02
  • @guy_m Then just use `post` instead of `postDelayed` – Tamim Attafi Feb 02 '23 at 14:32
  • Yeah sure. Somehow 10 years ago it was not clear to me :) now it is pretty obvious haha – guy_m Feb 03 '23 at 22:45
0

Please use this code in your manifest file

<activity
    android:name=".YourActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait" >
</activity>