37

I'm getting this exception on a 4.4.2 device. Not reproducible on Android 4.3 device or lower.

Setup is I have a home activity (subclass of support ActionBarActivity). The home activity checks a boolean flag, and if true, launches a splash screen activity (yes, ideally the splash comes before the home activity, but let's assume I can't change it to work that way for now).

The splash screen is launched with startActivityForResult, it downloads some config options from the server, then finishes and returns the result back to the home activity.

Weird thing is this works fine on 4.3 and below, but on 4.4 devices, I get the above exception (full stack trace):

02-21 13:36:16.733  24409-24409/test.player E/ActivityThread﹕ Performing stop of activity that is not resumed: {test.player/test.ui.actvities.HomeActivity}
    java.lang.RuntimeException: Performing stop of activity that is not resumed: {test.player/test.ui.actvities.HomeActivity}
            at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3147)
            at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3234)
            at android.app.ActivityThread.access$1100(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

Based on the above, it looks like onStop (because I launch the splash activity on onCreate) is called before onResume for the Home Activity.

Why is this now causing problems in 4.4.x?

donfuxx
  • 11,277
  • 6
  • 44
  • 76
user3072558
  • 723
  • 1
  • 6
  • 11
  • Is it about "settings/developer options/don't keep activities" selection? Is that option selected at 4.4.2 device and not selected at other one? – Devrim Feb 22 '14 at 00:15
  • Nope, that option is not enabled on both devices. – user3072558 Feb 22 '14 at 00:27
  • does your splash activity finish after downloading ? how do you go back to `HomeActivity` ? what is splash activity, is it a dialog ? – mangusta Feb 22 '14 at 00:34
  • Splash activity just extends regular Activity - it's full screen, not a dialog. After it finishes downloading the settings, it calls setResult and then finish(), which goes back to the home activity. – user3072558 Feb 22 '14 at 00:51
  • when you go to splash activity from `HomeActivity` or back to `HomeActivity` by finishing splash activity, `onStop()` of `HomeActivity` is not called. nothing is called actually, neither `onResume()` nor `onStop()` nor `onCreate()`. `onCreate()` has been executed in parallel with splash, so has been completed as well, at the moment when you're back in. – mangusta Feb 22 '14 at 01:59
  • 1
    That doesn't seem right to me. The splash activity would now be the top activity in the stack, so the the HomeActivity onStop lifecycle method would get called eventually. Coicidentally, I moved the startActivity call for the Splash activity from onCreate to onResume in the HomeActivity, and the error goes away. – user3072558 Feb 22 '14 at 02:55
  • Can you add that and accept it as an answer so this doesn't show up as an unanswered question? – nasch Apr 14 '14 at 14:32
  • @Devrim been fighting this problem for days. Would hang up after snapping pic with native Camera. Code works on Samsung 5.0 API 21, not on LG 6.0 API 23. Worked on rare occasion stepping through code so couldn't figure it out. Did not know about this dev setting on phone. Doesn't even exist on the phone that the code works correctly on. So unchecked box and everything worked! Hopefully others who have issue with code working on one phone and only sometimes on another and only when stepping through code in debug mode will get clued in to check this setting. – Kent Lauridsen Jun 22 '18 at 19:51

4 Answers4

20

That doesn't seem right to me. The splash activity would now be the top activity in the stack, so the HomeActivity onStop lifecycle method would get called eventually. Coincidentally, I moved the startActivity call for the splash activity from onCreate to onResume in the HomeActivity, and the error goes away.

Dalinaum
  • 1,142
  • 13
  • 18
user3072558
  • 723
  • 1
  • 6
  • 11
  • 1
    In my case still the issue is coming in onResume if i start another activity.What i had to do was to delay the start of activity with around 1000 milliseconds on onResume.Is there any better way around??? – sheetal Apr 23 '14 at 13:32
10

This issue will still be present on all high-end phones with Android 4.4.2 and above including NEXUS 5 and Samsumg s4 since onResume gets called but it is still in animation stage.So if you try to start a activity in onResume the issue will replicate.

Put your switching activity in a handler delayed method.

    Handler handler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
              //Start another Activity Here

            default:
                break;
        }
        return false;
    }
});

And in onResume call this.

 handler.sendEmptyMessageDelayed(1, 1000);

By that time you can show loader or something or block user Interaction

Felix
  • 128
  • 1
  • 12
sheetal
  • 3,014
  • 2
  • 31
  • 45
4

I was getting this exception even when using onResume(), so I ended up overriding onPostResume() and starting activity from there, and the exception is gone. Not sure if this is an ideal solution, but still...

Deinlandel
  • 1,023
  • 8
  • 25
3

Just call the onResume super method before launching the new activity:

super.onResume();
Chris623
  • 2,464
  • 1
  • 22
  • 30