Question

There are several threads running on the keywords "Activity restart". Honestly, I did my best before posting here.

In my application a background service monitors sensors (even when screen is OFF) and launches an activity. In order to Turn On the screen and show activity on lock screen am setting following flags

int flags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON   |
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.flags |= flags;

Everything goes perfect, Screen turns on, Activity displayed on lockscreen. But Activity is restarting. I do not know why ? This activity orientation is set to portrait. So change in orientation configuration should not restart activity. I could not find the reason for restart. What else might be the reason for restart ?

To avoid restarting I also set android:configChanges (to find the reason I included all possible configuration changes

  <activity android:label="@string/tstActivity_label" 
              android:name=".TstActivity" 
              android:screenOrientation="portrait" 
              android:noHistory="true" 
              android:excludeFromRecents="true" 
              android:configChanges="locale|keyboard|keyboardHidden|
                                     navigation|orientation|screenLayout|
                                     screenSize|smallestScreenSize|layoutDirection" >

    </activity>

Still no positive signs, activity restarts. Also onConfigurationChanged(Configuration newConfig) is never called.

Could some one help me how to find the reason for restart and how to avoid it ?

Why android:configChanges is being ignored.

Tested with API levels 14 and 15.

My main problem I am doing some actions in onStop() that should be only done when the activity is really stopping and not while it is restarting.

Thank you.

Note: I also tried with wakelocks and keygaurd manager, same story Activity is restarting.

Was it helpful?

Solution

The activity is not being "recreated", it is only being "restarted". This is a normal part of the activity lifecycle. If the activity was being recreated due to a config change, you would also see onDestroy() and onCreate() being called again. This is clearly not the case.

There is no configuration change going on here, which is why onConfigurationChanged() is not being called.

I think what is happening has to do with the fact that the screen is off/locked when your activity is launched. The activity is created and resumed, but then Android notices that the screen is off/locked, so it pauses the activity. Then it realizes that the activity should be shown, so it resumes the activity. I'm not able to test this case exactly right now, but I'm sure that I've seen similar behaviour in my own apps when we launch an activity while the screen is off.

Your app should be able to deal with multiple calls to onStart(), onStop(), onRestart(), onPause() and onResume() without getting confused. If not, you'll need to fix that.

OTHER TIPS

Use a handler. During onCreat() send a message to Handler with a delay of say 100 ms. With this time my activity goes paused and resumed again. Now when the message in received by the handler, start the actual functionality. In my case when the message is received, I start the music and in onStop() with out any condition, stop the music.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top