﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-不过而而-随笔分类-android</title><link>http://www.blogjava.net/lincode/category/49630.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 20 Sep 2011 13:18:35 GMT</lastBuildDate><pubDate>Tue, 20 Sep 2011 13:18:35 GMT</pubDate><ttl>60</ttl><item><title>[android] PhoneGap 在 android 下的实现原理</title><link>http://www.blogjava.net/lincode/archive/2011/09/20/359014.html</link><dc:creator>lincode</dc:creator><author>lincode</author><pubDate>Tue, 20 Sep 2011 02:20:00 GMT</pubDate><guid>http://www.blogjava.net/lincode/archive/2011/09/20/359014.html</guid><wfw:comment>http://www.blogjava.net/lincode/comments/359014.html</wfw:comment><comments>http://www.blogjava.net/lincode/archive/2011/09/20/359014.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lincode/comments/commentRss/359014.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lincode/services/trackbacks/359014.html</trackback:ping><description><![CDATA[<br />PhoneGap 是一个移动开发框架。通过 PhoneGap，开发者可以使用 JavaScript 调用手机的原生功能，例如，获取经纬度，让手机振动等。<br />主页&nbsp;http://www.phonegap.com/ 。<br />源码 https://github.com/phonegap/phonegap-android 。<br /><br />PhoneGap 在早期，应该是使用 WebView 的 addJavaScriptInterface 方法，来为 JS 提供调用原生功能可能。addJavaScriptInterface ，可以将一个 Java 对象绑定到一个 JS 对象。是的，JS对象可以调用 Java方法。但在 PhoneGap 1.0.0 这个版本中，PhoneGap 改变了方法。<br /><br />以振动功能为例，我们可以看一下程序调用的流程：<br /><strong><br />1 在 JS 中，启动命令</strong><br /><div>        <p>main.js / navigator.notification.vibrate(0);</p></div><div>         <p>notification.js / Notification.vibrate.vibrate 中执行了 PhoneGap.exec(null, null, "Notification", "vibrate", [mills]);</p></div>phonegap.js / PhoneGap.exc 中执行了&nbsp;var r = prompt(PhoneGap.stringify(args), "gap:"+PhoneGap.stringify([service, action, callbackId, true]));<br /><br />这时，WebView 就会企图弹出一个窗口。这时使用 android 提供的&nbsp;WebChromeClient 的&nbsp;API 就可以截获 WebView 的这个动作 。<br /><br /><strong>2 JAVA 中，处理命令<br /></strong>WebView 的 WebChromClient 实现了下面这个函数：<br /><div>        <p>public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result)</p></div>在 onJsPrompt 中执行了&nbsp;String r = pluginManager.exec(service, action, callbackId, message, async);<br /><br />PlugManager 会根据收到参数，将命令分发给特定的 Plugin。这个例子中，接收的 plugin 是：Notification。<br />落实到 Notification 的 exec 函数：会执行这一行：&nbsp;this.vibrate(args.getLong(0));<br /><br />振动的实现为：<br /><div>        <p>&nbsp;</p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;vibrate(</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;time){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Start&nbsp;the&nbsp;vibration,&nbsp;0&nbsp;defaults&nbsp;to&nbsp;half&nbsp;a&nbsp;second.</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;(time&nbsp;</span><span style="color: #000000; ">==</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">)&nbsp;{<br />time&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">500</span><span style="color: #000000; ">;<br />}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Vibrator&nbsp;vibrator&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;(Vibrator)&nbsp;</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.ctx.getSystemService(Context.VIBRATOR_SERVICE);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vibrator.vibrate(time);<br />}</span></div><p>&nbsp;</p></div><br /><strong>3 Java 处理完后的数据，需要给 JS 一个反馈：</strong><br />这里 PhoneGap 使用了一个在客户端本地实现的 XHRServer，具体到代码中就是一个JAVA 类 CallbackServer。<br /><br />分两个部分介绍其行为：<br />本地 XHRServer，<br />思想是，后台每执行完一个命令，都会将结果存在 CallbackServer 中的一个链表中，具体为CallbackServr的 private LinkedList&lt;String&gt; javascript;<br />这个结果其实是一段字符串表示的 JS 函数调用。例如检测网络调用的结果为：PhoneGap.callbackSuccess('Network Status1',{status:1,message:"wifi",keepCallback:true});<br />&nbsp;XHRServer 的行为很简单，只要有请求来，就把链表中的最先进来的提出来，返回给客户端。没有请求来，则 10秒钟返回一个空的回复，以维持XHRServer。<div></div>Webview 作为客户端：<br />在 WebView 中，会有一个轮询机制，这可以参考 PhoneGap.JSCallack 和&nbsp;PhoneGap.JSCallbackPolling 两个函数来访问 XHRServer。XHRServer，返回的结果就是 WebView 需要调用的 JS 函数。 在 JS 中，eval() 函数，将返回的结果变为一个可以执行的对象，在 Webview 中执行，可以认为这即是回调函数 Callback。这也是为什么 PhoneGap 为何命名 XHRServer 为 CallbackServer 的原因。<br /><br /><img src ="http://www.blogjava.net/lincode/aggbug/359014.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lincode/" target="_blank">lincode</a> 2011-09-20 10:20 <a href="http://www.blogjava.net/lincode/archive/2011/09/20/359014.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[android] Serializable 和 Parcelable 区别</title><link>http://www.blogjava.net/lincode/archive/2011/09/16/358805.html</link><dc:creator>lincode</dc:creator><author>lincode</author><pubDate>Fri, 16 Sep 2011 08:16:00 GMT</pubDate><guid>http://www.blogjava.net/lincode/archive/2011/09/16/358805.html</guid><wfw:comment>http://www.blogjava.net/lincode/comments/358805.html</wfw:comment><comments>http://www.blogjava.net/lincode/archive/2011/09/16/358805.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lincode/comments/commentRss/358805.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lincode/services/trackbacks/358805.html</trackback:ping><description><![CDATA[<br />android 中<span class="Apple-style-span" style="background-color: #ffffff; "><span class="Apple-style-span" style="color: #595959; font-family: Verdana, Arial, Helvetica, 宋体, sans-serif; line-height: 25px; "><span style="line-height: 21px; color: #333333; border-collapse: collapse; font-family: Arial; ">自定义的对象</span><span style="line-height: 28px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; ">序列化的问题</span><span style="line-height: 21px; color: #333333; border-collapse: collapse; font-family: Arial; ">有两个选择一个是Parcelable，另外一个是Serializable。</span></span><p style="font-family: Verdana, Arial, Helvetica, 宋体, sans-serif; line-height: 25px; color: #595959; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><span style="line-height: 21px; color: #333333; border-collapse: collapse; font-family: Arial; ">一 序列化原因：</span></p><p style="font-family: Verdana, Arial, Helvetica, 宋体, sans-serif; line-height: 25px; color: #595959; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><span style="line-height: 21px; color: #333333; border-collapse: collapse; "><span style="line-height: normal; color: #000000; font-size: 13px; border-collapse: separate; "><font face="宋体" style="line-height: 23px; ">1.永久性保存对象，保存对象的字节序列到本地文件中；<br style="line-height: 23px; " />2.通过序列化对象在网络中传递对象；<br style="line-height: 23px; " />3.通过序列化在进程间传递对象。&nbsp;<br /><br /></font></span></span></p><p style="font-family: Verdana, Arial, Helvetica, 宋体, sans-serif; line-height: 25px; color: #595959; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><span style="line-height: 21px; color: #333333; border-collapse: collapse; font-family: Arial; ">二 至于选取哪种可参考下面的原则：</span></p><p style="font-family: Verdana, Arial, Helvetica, 宋体, sans-serif; line-height: 25px; color: #595959; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><span style="line-height: 21px; color: #333333; border-collapse: collapse; font-family: Arial; ">1.在使用内存的时候，Parcelable 类比Serializable性能高，所以推荐使用Parcelable类。<br style="line-height: 25px; " />2.Serializable在序列化的时候会产生大量的临时变量，从而引起频繁的GC。<br style="line-height: 25px; " />3.Parcelable不能使用在要将数据存储在磁盘上的情况，因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点， 也不提倡用，但在这种情况下，还是建议你用Serializable 。</span></p></span><br /><strong>实现：</strong><br />1 Serializable 的实现，只需要继承 &nbsp;implements Serializable 即可。这只是给对象打了一个标记，系统会自动将其序列化。<br /><br />2 Parcelabel 的实现，需要在类中添加一个静态成员变量 CREATOR，这个变量需要继承 Parcelable.Creator 接口。<br /><div style="font-weight: bold; background-color: #eeeeee; font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;MyParcelable&nbsp;</span><span style="color: #0000FF; ">implements</span><span style="color: #000000; ">&nbsp;Parcelable&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">private</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;mData;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;describeContents()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;writeToParcel(Parcel&nbsp;out,&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;flags)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.writeInt(mData);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">static</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">final</span><span style="color: #000000; ">&nbsp;Parcelable.Creator</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">MyParcelable</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;CREATOR<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;Parcelable.Creator</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">MyParcelable</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;MyParcelable&nbsp;createFromParcel(Parcel&nbsp;in)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;MyParcelable(in);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;MyParcelable[]&nbsp;newArray(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;size)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;MyParcelable[size];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">private</span><span style="color: #000000; ">&nbsp;MyParcelable(Parcel&nbsp;in)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mData&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;in.readInt();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;}</span></div><br /><strong>&nbsp;</strong><img src ="http://www.blogjava.net/lincode/aggbug/358805.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lincode/" target="_blank">lincode</a> 2011-09-16 16:16 <a href="http://www.blogjava.net/lincode/archive/2011/09/16/358805.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[android] Activity 的生命周期 以及 横屏竖屏切换时 Activity 的状态变化</title><link>http://www.blogjava.net/lincode/archive/2011/09/16/358768.html</link><dc:creator>lincode</dc:creator><author>lincode</author><pubDate>Fri, 16 Sep 2011 02:32:00 GMT</pubDate><guid>http://www.blogjava.net/lincode/archive/2011/09/16/358768.html</guid><wfw:comment>http://www.blogjava.net/lincode/comments/358768.html</wfw:comment><comments>http://www.blogjava.net/lincode/archive/2011/09/16/358768.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lincode/comments/commentRss/358768.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lincode/services/trackbacks/358768.html</trackback:ping><description><![CDATA[<strong>生命周期</strong><br /><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">Android 系统在Activity 生命周期中加入一些钩子，我们可以在这些系统预留的钩子中做一些事情。<br />例举了 7 个常用的钩子：<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">protected void onCreate(Bundle savedInstanceState)<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">protected void onStart()<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">protected void onResume()<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">protected void onPause()<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">protected void onStop()<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">protected void onRestart()<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">protected void onDestroy()<br /><br />简要说明：<br /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">onCreate(Bundle savedInstanceState)：创建activity时调用。设置在该方法中，还以Bundle中可以提出用于创建该 Activity 所需的信息。<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">onStart()：activity变为在屏幕上对用户可见时，即获得焦点时，会调用。<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">onResume()：activity开始与用户交互时调用（无论是启动还是重新启动一个活动，该方法总是被调用的）。<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">onPause()：activity被暂停或收回cpu和其他资源时调用，该方法用于保存活动状态的。。<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">onStop()：activity被停止并转为不可见阶段及后续的生命周期事件时，即失去焦点时调用。<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">onRestart()：重新启动activity时调用。该活动仍在栈中，而不是启动新的活动。<br style="line-height: 22px; " /></span><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; background-color: #ffffff; ">onDestroy()：activity被完全从系统内存中移除时调用，该方法被调用可能是因为有人直接调用 finish()方法 或者系统决定停止该活动以释放资源。<br /></span><br /><strong>横竖屏切换</strong><br /><br />1 切换到横屏<br /><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onSaveInstanceState<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onPause<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onStop<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onDestroy<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onCreate<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onStart<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onRestoreInstanceState<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onResume<br /><br /></span>2 切换到竖屏，销毁了两次<br /><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onSaveInstanceState<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onPause<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onStop<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onDestroy</span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onCreate<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onStart<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onRestoreInstanceState<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onResume<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onSaveInstanceState<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onPause<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onStop<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onDestroy<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onCreate<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onStart<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onRestoreInstanceState<br /></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #ffffff; ">onResume</span><br /><br /><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #f5fafe; "><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">3 修改AndroidManifest.xml，把该Activity添加 android:configChanges="orientation"，切横屏，只销毁一次。</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">onSaveInstanceState<br />onPause<br />onStop<br />onDestroy<br />onCreate<br />onStart<br />onRestoreInstanceState<br />onResume</p></span>4&nbsp;<span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #f5fafe; ">再切回竖屏，发现不会再打印相同信息，但多打印了一行onConfigChanged<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">onSaveInstanceState<br />onPause<br />onStop<br />onDestroy<br />onCreate<br />onStart<br />onRestoreInstanceState<br />onResume<br />onConfigurationChanged</p></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #f5fafe; "><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">5 更改 android:configChanges="orientation" 改成 android:configChanges="orientation|keyboardHidden"，切横屏，就只打印onConfigChanged</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">onConfigurationChanged</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">6 切回竖屏</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">onConfigurationChanged<br />onConfigurationChanged</p></span><span class="Apple-style-span" style="font-family: Arial, 宋体; line-height: 26px; background-color: #f5fafe; "><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">总结：</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">1、不设置Activity的android:configChanges时，切屏会重新调用各个生命周期，切横屏时会执行一次，切竖屏时会执行两次</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">2、设置Activity的android:configChanges="orientation"时，切屏还是会重新调用各个生命周期，切横、竖屏时只会执行一次</p><p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">3、设置Activity的android:configChanges="orientation|keyboardHidden"时，切屏不会重新调用各个生命周期，只会执行onConfigurationChanged方法</p></span><br /><br /><br /><br /><img src ="http://www.blogjava.net/lincode/aggbug/358768.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lincode/" target="_blank">lincode</a> 2011-09-16 10:32 <a href="http://www.blogjava.net/lincode/archive/2011/09/16/358768.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>