天天看点

防止事件导致的oncreate的多次调用

<code>&lt;activity android:name=</code><code>".Main"</code>

<code>                  </code><code>android:label=</code><code>"@string/app_name"</code> <code>android:configChanges=</code><code>"orientation|keyboardHidden"</code><code>&gt;</code>

<code>public</code> <code>void</code> <code>onConfigurationChanged(Configuration newConfig) {</code>

<code>        </code><code>super</code><code>.onConfigurationChanged(newConfig);</code>

<code>}</code>

<code>现在笔者正在moto702上测试游戏demo,突然发现滑盖会导致当前Activity多次调用oncreate事件。由于游戏的surfaceview是这里初始化的,所以多次的调用一定会给游戏本身带来多次的初始化。怎么解决呢 在AndroidManifest.xml中修改如上xml的内容android:configChanges=</code><code>"orientation|keyboardHidden"</code><code>并且在当前Activity中重载方法onConfigurationChanged,有需要的可以在方法中实现自己的功能。如:</code>

<code>/*</code>

<code>         </code><code>*</code>

<code>         </code><code>* if (this.getResources().getConfiguration().orientation ==</code>

<code>         </code><code>* Configuration.ORIENTATION_LANDSCAPE) {</code>

<code>         </code><code>* }</code>

<code>         </code><code>* else if (this.getResources().getConfiguration().orientation ==</code>

<code>         </code><code>* Configuration.ORIENTATION_PORTRAIT) {</code>

<code>         </code><code>*/ 本文转自java豆子博客园博客,原文链接:http://www.cnblogs.com/error404/archive/2011/08/03/2126041.html,如需转载请自行联系原作者</code>

继续阅读