Prevent Android activity from being recreated on turning screen off

随声附和 提交于 2019-11-28 10:51:03
Twice Circled

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

add:

android:launchMode="singleTop"

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

noober
  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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!