3.创建Activity一般人所不知道的地方
1)Activity里的各个生命周期的方法一般执行什么代码
在onCreate()方法里,对于高版本的API,应判断一下系统的的版本再决定是否执行。
<code>// Make sure we're running on Honeycomb or higher to use ActionBar APIs</code>
<code> </code><code>if</code> <code>(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {</code>
<code> </code><code>// For the main activity, make sure the app icon in the action bar</code>
<code> </code><code>// does not behave as a button</code>
<code> </code><code>ActionBar actionBar = getActionBar();</code>
<code> </code><code>actionBar.setHomeButtonEnabled(</code><code>false</code><code>);</code>
<code> </code><code>}</code>
As long as the activity is still partially visible but currently not the activity in focus, it remains paused.(Activity可见,但是没有获得焦点就会进行pause状态)
Stop animations or other ongoing actions that could consume CPU.(停止动画)
Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.(释放网络资源)
上面的文字中着重强调了,如果想要快速的跳转到下一个Activity,不要将一些重量级的操作放在onPause里,而应该放在onStop方法中。由下图也可以领会这个意思:
<a href="http://s3.51cto.com/wyfs02/M02/76/5B/wKioL1ZQlH6BUzbeAAA-mVJomWI765.png" target="_blank"></a>
一般初始化相机的方法写在onResume()方法中,release camera的逻辑写在onPause中。
Google重点强调了onRestart与onStart的区别,强烈要求onStart()与onStop配对使用,因为onRestart只有在从stop状态返回的时候被调用,在Activity创建的时候并不会被调用。
2)保存Activity中View的信息
Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout). (比如在切屏的时候,Activity会销毁,但Activity组件数据会消除怎么办?)
具体怎么保存View的状态,View的状态又会传到哪儿去?
在代码里保存信息
<code>@Override</code>
<code>protected</code> <code>void</code> <code>onCreate(Bundle savedInstanceState) {</code>
<code> </code><code>super</code><code>.onCreate(savedInstanceState); </code><code>// Always call the superclass first</code>
<code> </code>
<code> </code><code>// Check whether we're recreating a previously destroyed instance</code>
<code> </code><code>if</code> <code>(savedInstanceState != </code><code>null</code><code>) {</code>
<code> </code><code>// Restore value of members from saved state</code>
<code> </code><code>mCurrentScore = savedInstanceState.getInt(STATE_SCORE);</code>
<code> </code><code>mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);</code>
<code> </code><code>} </code><code>else</code> <code>{</code>
<code> </code><code>// Probably initialize members with default values for a new instance</code>
<code> </code><code>...</code>
<code>}</code>
取出信息
<code>public</code> <code>void</code> <code>onRestoreInstanceState(Bundle savedInstanceState) {</code>
<code> </code><code>// Always call the superclass so it can restore the view hierarchy</code>
<code> </code><code>super</code><code>.onRestoreInstanceState(savedInstanceState);</code>
<code> </code><code>// Restore state members from saved instance</code>
<code> </code><code>mCurrentScore = savedInstanceState.getInt(STATE_SCORE);</code>
<code> </code><code>mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);</code>
<code></code>
本文转自屠夫章哥 51CTO博客,原文链接:http://blog.51cto.com/4259297/1715641,如需转载请自行联系原作者