18

How to prevent an activity from being recreated on turning screen off?

What I do

  1. Start Bejewels and go to the jewelry screen.
  2. Press power button shortly. The screen is turned off, but the device is not.
  3. Press power button again.

What I see

The same screen as before turning screen off.

In case of my application (trivial one, just a web-app with a single WebView) the scenario is the following:

What I do

  1. Start my app. The activity onCreate() method loads an URL into the WebView.
  2. Press power button shortly. The screen is turned off, but the device is not.
  3. Press power button again.

What I see

The WebView is reloading the page.

What I expected

As in Bejewels case I want to see the same screen, that for my app is the page in its previous state: scripts are running, a form fields are filled etc.

Debugging showed, that I was wrong (in a previous question) and onDestroy() and onCreate() are being called one-by-one when the screen is just turned on. And since I still hear a music, played by a script, when the screen is off, it looks like both the activity and the WebView do exist until I press power button again.

What I tried.

  1. android:alwaysRetainTaskState="true" The same behavior.
  2. Reading about intents (to no avail, I just did not understand, how they are applied to my situation).
  3. Using PhoneGap. It behaves differently: just kills the whole application on pressing power button. This IS better, but not the best.
noober
  • 4,819
  • 12
  • 49
  • 85

3 Answers3

29

The solution to this problem may be the same as that described here: onDestroy gets called each time the screen goes on

It sounds like your activity is being restarted due to configuration changes, see http://developer.android.com/guide/topics/resources/runtime-changes.html. The most common is when your app is in landscape mode (like most games) and then the screen lock is engaged by tapping the power button. The screen-lock is in portrait mode and therefore triggers a change in orientation which triggers the activity to be restarted.

This behaviour can be overridden by adding:

android:configChanges="orientation|keyboardHidden"

... to your manifest file if you are targeting API level less than 13. Or

android:configChanges="orientation|keyboardHidden|screenSize"

... if you are targeting API level greater than 13.

Note you may need to go project->properties and update your project build target. 'screenSize' will not be recognised if your build target is less than 13.

It may be a different configuration change that is causing the activity to be reset. The following link provides a list of the possible configuration changes: http://developer.android.com/guide/topics/manifest/activity-element.html#config

Community
  • 1
  • 1
Twice Circled
  • 1,410
  • 1
  • 15
  • 23
1

add:

android:launchMode="singleTop"

to the activity part in the manifest xml. see here http://developer.android.com/guide/topics/manifest/activity-element.html

Bush
  • 2,433
  • 5
  • 34
  • 57
  • Nothing has changed. (I didn't remove android:alwaysRetainTaskState="true"). – noober May 08 '12 at 13:14
  • after pressing power button to screen off and then again to screen on the onResume() is called, what are you doing in onResume()? – Bush May 08 '12 at 13:24
  • 1. Yes, onResume() is called. I've checked it (overridden and called Toast). 2. Sorry for being dumb, I just didn't guess I had to do something in onResume(). So, it was not overridden and now it contains just Toast call. 3. Behavior now is the following: after pressing the button first time, when screen is off, the page is reloading in background (the music stops to play then plays from start, there is a javascript in onload section of web page that starts music playing). – noober May 08 '12 at 13:41
1
  1. Create the view in Application.onCreate().
  2. Add the view to a layout in Activity.onCreate().
  3. Remove the view from the layout in Activity.onDestroy().

Details are here: Attach/detach Android view to/from layout

Community
  • 1
  • 1
noober
  • 4,819
  • 12
  • 49
  • 85