Android基礎-Activity基本使用
1. Activity概念
Activity,即活動,是一種可以包含使用者介面的元件,主要用於和使用者進行互動
活動提供應用程式繪製其UI的視窗。此視窗通常填充螢幕,但可能小於螢幕並浮動在其他視窗的頂部。通常,一個活動在應用程式中實現一個螢幕。
多數應用包含多個螢幕,即多個活動,一般會指定一個活動為主要活動,即使用者啟動程式顯示的第一個螢幕。
2. Activity基本用法
2.1 註冊宣告
所有的活動都要在AndroidManifest.xml中進行註冊才能生效活動的註冊宣告要放在<application>
標籤內,在<activity>
標籤中是用了android:name來指定具體註冊哪一個活動。
**配置主活動的方法**就是在<application>
標籤內加入<intent-filter>
標籤。這個標籤內包含和兩個可選標籤、。這些標籤元素組合在一起以指定您的活動可以響應的意圖型別。
示例:
<span style="color:#000000"><code class="language-xml"><span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>activity</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>.ExampleActivity<span style="color:#999999">"</span></span> <span style="color:#d19a66">android:icon</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>@drawable/app_icon<span style="color:#999999">"</span></span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>intent-filter</span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>action</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>android.intent.action.SEND<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>category</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>android.intent.category.DEFAULT<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>data</span> <span style="color:#d19a66">android:mimeType</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>text/plain<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>intent-filter</span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>activity</span><span style="color:#999999">></span></span>
</code></span>
如果時配置成主活動的話,為:
<span style="color:#000000"><code class="language-xml"><span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>action</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>android.intent.action.MAIN<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>category</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>android.intent.category.LAUNCHER<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
</code></span>
2.2 許可權宣告
除非兩個活動在其清單中具有相同的許可權,否則父活動無法啟動子活動。如果為父活動宣告uses-permission元素,則每個子活動必須具有匹配的uses-permission元素。
示例:
<span style="color:#000000"><code class="language-xml"><span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>manifest</span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>activity</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>....<span style="color:#999999">"</span></span>
<span style="color:#d19a66">android:permission</span><span style="color:#669900"><span style="color:#999999">=</span>”com.google.socialapp.permission.SHARE_POST”</span>
<span style="color:#999999">/></span></span>
</code></span>
如果父活動宣告瞭該許可權,則子活動也必須宣告該許可權,否則父活動無法啟動子活動。
2.3 銷燬一個活動
銷燬一個活動只需要按back鍵即可。Activity類提供了一個finish()方法,活動呼叫這個方法就可銷燬當前活動。
2.4 Intent使用
Intent是Android程式中各元件之間進行互動的一種重要方式,不僅可以指明當前元件想要執行的動作,還可以在不同元件之間進行傳遞資料。被用於啟動活動,啟動服務以及傳送廣播等場景。
2.4.1 使用顯式Intent
Activity類中提供一個startActivity()
方法,專門用於啟動活動。
顯示啟動的三種方法:
-
基本用法
<span style="color:#000000"><code class="language-java">Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span>FirstActivity<span style="color:#999999">.</span><span style="color:#c678dd">this</span><span style="color:#999999">,</span> SecondActivity<span style="color:#999999">.</span><span style="color:#c678dd">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#61aeee">startActivity</span><span style="color:#999999">(</span>intent<span style="color:#999999">)</span><span style="color:#999999">;</span> </code></span>
-
通過Intent的ComponentName啟動
<span style="color:#000000"><code class="language-java">ComponentName cn <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> ComponentName<span style="color:#999999">(</span><span style="color:#669900">"com.baiheng.activitytest"</span><span style="color:#999999">,</span><span style="color:#669900">"com.baiheng.activitytest.ThirdActivity"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span> intent<span style="color:#999999">.</span><span style="color:#61aeee">setComponent</span><span style="color:#999999">(</span>cn<span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#61aeee">startActivity</span><span style="color:#999999">(</span>intent<span style="color:#999999">)</span><span style="color:#999999">;</span> </code></span>
-
通過初始化Intent時指定包名
<span style="color:#000000"><code class="language-java">Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span><span style="color:#669900">"android.intent.action.MAIN"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> intent<span style="color:#999999">.</span><span style="color:#61aeee">setClassName</span><span style="color:#999999">(</span><span style="color:#669900">"com.baiheng.activitytest"</span><span style="color:#999999">,</span> <span style="color:#669900">"com.baiheng.activitytest.ThirdActivity"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#61aeee">startActivity</span><span style="color:#999999">(</span>intent<span style="color:#999999">)</span><span style="color:#999999">;</span> </code></span>
2.4.2 使用隱式Intent
不明確指出我們想要啟動哪一個活動,指定一系列抽象的action和category等資訊,交由系統分析這個Intent,這個通過Intent的Intent-filter是實現。
<span style="color:#000000"><code class="language-xml"><span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>activity</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>.SecondActivity<span style="color:#999999">"</span></span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>intent-filter</span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>action</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>com.baiheng.activitytest.action.START<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>category</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>android.intent.category.DEFAULT<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>intent-filter</span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>activity</span><span style="color:#999999">></span></span>
</code></span>
<span style="color:#000000"><code class="language-java">Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
intent<span style="color:#999999">.</span><span style="color:#61aeee">setAction</span><span style="color:#999999">(</span>Intent<span style="color:#999999">.</span>ACTION_START<span style="color:#999999">)</span><span style="color:#999999">;</span> intent<span style="color:#999999">.</span><span style="color:#61aeee">addCategory</span><span style="color:#999999">(</span>Intent<span style="color:#999999">.</span>CATEGORY_DEFAULT<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">startActivity</span><span style="color:#999999">(</span>intent<span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>
每個Intent只能指定一個action,但是卻能指定多個category。
在<intent-filter>
標籤中再配置一個<data>
標籤,用於更精準地指定當前活動能夠響應什麼型別的資料。<data>
標籤中主要可以配置一下內容:
- android:scheme:用於指定資料的協議部分,如示例:http
- android:host:用於指定資料的主機名部分,如示例www.baidu.com
- android:port:用於指定資料的埠部分,一般緊隨在主機名之後
- android:path:用於指定主機名和埠之後的部分,如一段網址跟在主機名之後的內容
- android:mimeType:用於指定可以處理的資料型別,允許使用萬用字元的方式進行指定。
只有<data>
標籤中指定的內容和Intent中攜帶的Data完全一致,當前活動才能夠相應該Intent。一般不會在<data>
標籤中指定過多內容。
2.4.3 使用隱式Intent啟動其他程式
使用隱式Intent不僅可以啟動程式內的活動,還可以啟動其他程式的活動。
示例:
<span style="color:#000000"><code class="language-java">Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span>Intent<span style="color:#999999">.</span>ACTION_VIEW<span style="color:#999999">)</span><span style="color:#999999">;</span>
intent<span style="color:#999999">.</span><span style="color:#61aeee">setData</span><span style="color:#999999">(</span>Uri<span style="color:#999999">.</span><span style="color:#61aeee">parse</span><span style="color:#999999">(</span><span style="color:#669900">"http://www.google.com.hk"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">startActivity</span><span style="color:#999999">(</span>intent<span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>
Uri.parse
將一個網址字串解析成一個Uri物件。
<span style="color:#000000"><code class="language-xml"><span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>activity</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>.ThirdActivity<span style="color:#999999">"</span></span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>intent-filter</span> <span style="color:#d19a66">tools:ignore</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>AppLinkUrlError<span style="color:#999999">"</span></span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>action</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>android.intent.action.VIEW<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>category</span> <span style="color:#d19a66">android:name</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>android.intent.category.DEFAULT<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>data</span> <span style="color:#d19a66">android:scheme</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>http<span style="color:#999999">"</span></span> <span style="color:#999999">/></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>intent-filter</span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>activity</span><span style="color:#999999">></span></span>
</code></span>
3. Activity 間資料傳遞
一個App一般包含多個Activity,涉及到Activity之間的資料傳遞。關於如何傳遞不同的資料型別在Intent部分詳解,這裡介紹一下兩種傳遞方式
3.1 向下傳遞資料
Intent中提供了一些列putExtra()
方法的過載,可以把想要傳遞的資料暫存在Intent中,啟動了另一個活動後,把這些資料從Intent中取出來就可以。
示例:在FirstActivity中:
<span style="color:#000000"><code class="language-java">String data <span style="color:#669900">=</span> <span style="color:#669900">"Hello SecondActivity"</span><span style="color:#999999">;</span>
Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span>FirstActivity<span style="color:#999999">.</span><span style="color:#c678dd">this</span><span style="color:#999999">,</span> SecondActivity<span style="color:#999999">.</span><span style="color:#c678dd">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
intent<span style="color:#999999">.</span><span style="color:#61aeee">putExtra</span><span style="color:#999999">(</span><span style="color:#669900">"extra_data"</span><span style="color:#999999">,</span> data<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">startActivity</span><span style="color:#999999">(</span>intent<span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>
SecondActivity:
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onCreate</span><span style="color:#999999">(</span>Bundle savedInstanceState<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">super</span><span style="color:#999999">.</span><span style="color:#61aeee">onCreate</span><span style="color:#999999">(</span>savedInstanceState<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">setContentView</span><span style="color:#999999">(</span>R<span style="color:#999999">.</span>layout<span style="color:#999999">.</span>second_activity<span style="color:#999999">)</span><span style="color:#999999">;</span>
Intent intent <span style="color:#669900">=</span> <span style="color:#61aeee">getIntent</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
String data <span style="color:#669900">=</span> intent<span style="color:#999999">.</span><span style="color:#61aeee">getStringExtra</span><span style="color:#999999">(</span><span style="color:#669900">"extra_data"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Log<span style="color:#999999">.</span><span style="color:#61aeee">d</span><span style="color:#999999">(</span><span style="color:#669900">"SecondActivity"</span><span style="color:#999999">,</span> data<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
3.2 返回資料給上一個活動
Activity中還有一個startActivityForResult()
方法也適用於啟動活動,但是該方法期望活動銷燬的時候返回一個結果給上一個活動。
該方法接收兩個引數,一個是Intent,第二個引數是請求碼,用於之後在回撥中判斷資料的來源。
FirstActivity:
<span style="color:#000000"><code class="language-java">Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span>FirstActivity<span style="color:#999999">.</span><span style="color:#c678dd">this</span><span style="color:#999999">,</span> SecondActivity<span style="color:#999999">.</span><span style="color:#c678dd">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">startActivityForResult</span><span style="color:#999999">(</span>intent<span style="color:#999999">,</span><span style="color:#98c379">1</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>
SecondActivity:
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onCreate</span><span style="color:#999999">(</span>Bundle savedInstanceState<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">super</span><span style="color:#999999">.</span><span style="color:#61aeee">onCreate</span><span style="color:#999999">(</span>savedInstanceState<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">setContentView</span><span style="color:#999999">(</span>R<span style="color:#999999">.</span>layout<span style="color:#999999">.</span>second_activity<span style="color:#999999">)</span><span style="color:#999999">;</span>
Button button2 <span style="color:#669900">=</span> <span style="color:#999999">(</span>Button<span style="color:#999999">)</span> <span style="color:#61aeee">findViewById</span><span style="color:#999999">(</span>R<span style="color:#999999">.</span>id<span style="color:#999999">.</span>button_2<span style="color:#999999">)</span><span style="color:#999999">;</span>
button2<span style="color:#999999">.</span><span style="color:#61aeee">setOnClickListener</span><span style="color:#999999">(</span><span style="color:#c678dd">new</span> View<span style="color:#999999">.</span>OnClickListener<span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#999999">@Override</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onClick</span><span style="color:#999999">(</span>View v<span style="color:#999999">)</span> <span style="color:#999999">{</span>
Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
intent<span style="color:#999999">.</span><span style="color:#61aeee">putExtra</span><span style="color:#999999">(</span><span style="color:#669900">"data_return"</span><span style="color:#999999">,</span><span style="color:#669900">"Hello FirstActivity"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">setResult</span><span style="color:#999999">(</span>RRSULT_OK<span style="color:#999999">,</span> intent<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">finish</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
最後在FirstActivity中重寫這個方法得到返回資料:
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onActivityResult</span><span style="color:#999999">(</span><span style="color:#c678dd">int</span> requestCode<span style="color:#999999">,</span> <span style="color:#c678dd">int</span> resultCode<span style="color:#999999">,</span> <span style="color:#999999">@Nullable</span> Intent data<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">switch</span><span style="color:#999999">(</span>requestCode<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">case</span> <span style="color:#98c379">1</span><span style="color:#669900">:</span>
<span style="color:#c678dd">if</span> <span style="color:#999999">(</span>resultCode <span style="color:#669900">==</span> RESULT_OK<span style="color:#999999">)</span> <span style="color:#999999">{</span>
String returnedData <span style="color:#669900">=</span> data<span style="color:#999999">.</span><span style="color:#61aeee">getStringExtra</span><span style="color:#999999">(</span><span style="color:#669900">"data_return"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Log<span style="color:#999999">.</span><span style="color:#61aeee">d</span><span style="color:#999999">(</span><span style="color:#669900">"FritstActivity"</span><span style="color:#999999">,</span>returnedData<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">break</span><span style="color:#999999">;</span>
<span style="color:#c678dd">default</span><span style="color:#669900">:</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
如果是通過back鍵返回,則可以通過在SecondActivity中重寫onBackPressed()
來解決:
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Override</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onBackPressed</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
intent<span style="color:#999999">.</span><span style="color:#61aeee">putExtra</span><span style="color:#999999">(</span><span style="color:#669900">"data_return"</span><span style="color:#999999">,</span><span style="color:#669900">"Hello FirstActivity"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">setResult</span><span style="color:#999999">(</span>RESULT_OK<span style="color:#999999">,</span> intent<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#61aeee">finish</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
4. Activity生命週期
4.1 返回棧
Android中的活動是可以層疊的。每啟動一個新的活動,就會覆蓋原來的活動之上,點選Back鍵就會銷燬最上面的活動。
Android是使用任務(Task)來管理活動的,一個任務就是一組存放在棧裡的活動的集合,這個棧也稱作返回棧(Back Stack)。系統總是會顯示處於棧頂活動給使用者。
4.2 活動狀態
每個活動在其生命週期中最多可能會有4中狀態:
-
執行狀態:
活動位於返回棧的棧頂時,這時活動就處於執行狀態。
-
暫停狀態:
活動不處於棧頂時,但是仍然可見,活動就進入暫停狀態。
-
停止狀態:
活動不處於棧頂位置,且完全不可見,就進入停止狀態。處於停止狀態的活動有可能會被系統回收
-
銷燬狀態:
活動從返回棧中移除後就變成了銷燬狀態。
4.3 生命週期
Activity類中定義了7種回撥方法,覆蓋了活動生命週期的每一個環節:
onCreate()
:完成活動的初始化操作,比如載入佈局,繫結事件onStart()
:在活動由不可見變為可見的時候呼叫onResume()
:在活動準備好和使用者進行互動時呼叫。該活動一定處於返回棧棧頂,且處於執行狀態onPause()
:系統準備去啟動或者恢復另一個活動的時候呼叫。該方法的執行速度一定要快,否則會影響到新的棧頂活動的使用onStop
:在活動完全不可見時呼叫,與前者的區別時啟動的新活動是一個對話方塊式的活動,那麼onPause()方法會得到執行,而該方法不會onDestory()
:在活動被銷燬之前呼叫,之後活動變為銷燬狀態onRestart()
:由停止狀態變為執行時狀態之前呼叫,活動被重新啟動。
除了**onRestart()**方法之外,其他都是兩兩相對的,活動分為3中生存期:
- 完整生存期:活動在
onCreate()
和onDestrory()
之間經歷的就是完整生存期 - 可見生存期:活動在
onStart()
和onStop
之間經歷的就是可見生存期。活動對於使用者總是可見的,即便有可能無法和使用者進行互動。 - 前臺生存期:活動在
onResume()
和onPause()
之間經歷的就是前臺生存期。活動總是處於執行狀態,此時活動可以和使用者進行互動
[外鏈圖片轉存失敗(img-SiLfeKn5-1565323366159)(https://developer.android.com/guide/components/images/activity_lifecycle.png)]
4.4 活動被回收
如果一個活動進入停止狀態,有可能被系統回收,其中的資料在回收時可能得不到儲存。
Activity中提供了一個onSaveInstanceState()
回撥方法,這個方法可以保證活動被回收之前一定被呼叫,可以通過這個方法來儲存資料。
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onSaveInstanceState</span><span style="color:#999999">(</span>Bundle outState<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">super</span><span style="color:#999999">.</span><span style="color:#61aeee">onSaveInstanceState</span><span style="color:#999999">(</span>outState<span style="color:#999999">)</span><span style="color:#999999">;</span>
String tempData <span style="color:#669900">=</span> <span style="color:#669900">"Something you just typed"</span><span style="color:#999999">;</span>
outState<span style="color:#999999">.</span><span style="color:#61aeee">putString</span><span style="color:#999999">(</span><span style="color:#669900">"data_key"</span><span style="color:#999999">,</span> tempData<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
資料的取值可以在onCreate()
方法中取出
<span style="color:#000000"><code class="language-java"><span style="color:#c678dd">if</span> <span style="color:#999999">(</span>savedInstanceState <span style="color:#669900">!=</span> null<span style="color:#999999">)</span> <span style="color:#999999">{</span>
String tempData <span style="color:#669900">=</span> savedInstanceState<span style="color:#999999">.</span><span style="color:#61aeee">getString</span><span style="color:#999999">(</span><span style="color:#669900">"data_key"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Log<span style="color:#999999">.</span><span style="color:#61aeee">d</span><span style="color:#999999">(</span>TAG<span style="color:#999999">,</span> tempData<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
5. 橫豎屏切換
App橫豎屏切換的時候會銷燬當前的Activity然後重新建立一個,橫豎屏切換時Activity走下述生命週期:
onPause-> onStop-> onDestory-> onCreate->onStart->onResume
禁止橫豎屏自動切換的話,在AndroidManifest.xml中為Activity新增一個屬性:android:screenOrientaiton
6. 啟動模式
啟動模式一共有4種,分別是:standard
,singleTop
,singleTask
和singleInstance
。可以再AndroidManifest.xml中通過<activity>
標籤指定android:launchMode屬性來選擇啟動模式。
6.1 standard
預設模式,每當啟動一個新活動,就會在返回棧中入棧,並處於棧頂位置。該模式的活動無論這個活動是否在返回棧中存在,每次啟動都會建立該活動的新例項。
6.2 singleTop
當活動指定為singleTop模式,啟動活動時如果發現返回棧的棧頂已經是該活動,則認為可以直接使用它,不會再建立新的活動例項。
6.2 singleTask
當活動的啟動模式指定為singleTask,每次啟動該活動系統首先會在返回棧中檢查是否存在該活動的例項,如果發現就直接使用,並把這個活動之上的所有活動統統出棧。如果沒有發現就建立一個新的活動例項。
6.4 singleInstance
指定為singleInstance模式的活動會啟用一個新的返回棧來管理這個活動(如果singleTask模式指定了不同的taskAffinity,也會啟動一個新的返回棧)。這種模式解決了其他程式和我們的程式共享一個活動的例項
7. 簡單技巧
7.1 快速定位某個介面對應的活動
新建一個Java類:BaseActivity,繼承AppCompatActivity,重寫onCreate()方法。
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onCreate</span><span style="color:#999999">(</span><span style="color:#999999">@Nullable</span> Bundle savedInstanceState<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">super</span><span style="color:#999999">.</span><span style="color:#61aeee">onCreate</span><span style="color:#999999">(</span>savedInstanceState<span style="color:#999999">)</span><span style="color:#999999">;</span>
Log<span style="color:#999999">.</span><span style="color:#61aeee">d</span><span style="color:#999999">(</span><span style="color:#669900">"BaseActivity"</span><span style="color:#999999">,</span> <span style="color:#61aeee">getClass</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">getSimpleName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
在oncreate()
方法中獲取了當前例項的類名,然後將專案中所有活動的父類都不再繼承AppCompatActivity,而是繼承BaseActivity,這樣檢視日誌,,每當進入一個活動的介面,該活動的類名就被列印出來。
7.2 隨時退出程式
按home鍵只是將程式掛起,並沒有退出程式,如果程式需要一個登出或者退出的功能,只需要一個專門的集合類對所有的活動進行管理就可以。
新建一個ActivityCollector類作為活動管理器:
<span style="color:#000000"><code class="language-java"><span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> ActivityCollector <span style="color:#999999">{</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">static</span> List<span style="color:#61aeee"><span style="color:#999999"><</span>Activity<span style="color:#999999">></span></span> activities <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> ArrayList<span style="color:#669900"><</span><span style="color:#669900">></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">static</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">addActivity</span><span style="color:#999999">(</span>Activity activity<span style="color:#999999">)</span> <span style="color:#999999">{</span>
activities<span style="color:#999999">.</span><span style="color:#61aeee">add</span><span style="color:#999999">(</span>activity<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">static</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">removeActivity</span><span style="color:#999999">(</span>Activity activity<span style="color:#999999">)</span> <span style="color:#999999">{</span>
activities<span style="color:#999999">.</span><span style="color:#61aeee">remove</span><span style="color:#999999">(</span>activity<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">static</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">finishAll</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">for</span> <span style="color:#999999">(</span>Activity activity <span style="color:#669900">:</span> activities<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">if</span> <span style="color:#999999">(</span><span style="color:#669900">!</span>activity<span style="color:#999999">.</span><span style="color:#61aeee">isFinishing</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
activity<span style="color:#999999">.</span><span style="color:#61aeee">finish</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
修改BaseActivity
<span style="color:#000000"><code class="language-java"><span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> BaseActivity <span style="color:#c678dd">extends</span> AppCompatActivity <span style="color:#999999">{</span>
<span style="color:#999999">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onCreate</span><span style="color:#999999">(</span><span style="color:#999999">@Nullable</span> Bundle savedInstanceState<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">super</span><span style="color:#999999">.</span><span style="color:#61aeee">onCreate</span><span style="color:#999999">(</span>savedInstanceState<span style="color:#999999">)</span><span style="color:#999999">;</span>
Log<span style="color:#999999">.</span><span style="color:#61aeee">d</span><span style="color:#999999">(</span><span style="color:#669900">"BaseActivity"</span><span style="color:#999999">,</span> <span style="color:#61aeee">getClass</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">getSimpleName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
ActivityCollector<span style="color:#999999">.</span><span style="color:#61aeee">addActivity</span><span style="color:#999999">(</span><span style="color:#c678dd">this</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onDestroy</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">super</span><span style="color:#999999">.</span><span style="color:#61aeee">onDestroy</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
ActivityCollector<span style="color:#999999">.</span><span style="color:#61aeee">removeActivity</span><span style="color:#999999">(</span><span style="color:#c678dd">this</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
如果需要在某個地方退出程式,呼叫ActivityCollector.finishAll()
方法就可以。
可以再銷燬所有活動程式碼的後面加上殺掉當前程式,確保程式完全退出:
<span style="color:#000000"><code class="language-java">android<span style="color:#999999">.</span>os<span style="color:#999999">.</span>Process<span style="color:#999999">.</span><span style="color:#61aeee">killProcess</span><span style="color:#999999">(</span>android<span style="color:#999999">.</span>os<span style="color:#999999">.</span>Process<span style="color:#999999">.</span><span style="color:#61aeee">myPid</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>
- 1
7.2啟動活動最佳寫法
如果你的模組需要將一些重要資料傳遞給另一個模組,例如兩個字串引數,但是你不清楚啟動這個活動需要傳遞哪些資料,只需在啟動活動新增一個actionStart()
方法。
<span style="color:#000000"><code class="language-java"><span style="color:#c678dd">public</span> <span style="color:#c678dd">static</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">actionStart</span><span style="color:#999999">(</span>Context context<span style="color:#999999">,</span> String data1<span style="color:#999999">,</span> String data2<span style="color:#999999">)</span> <span style="color:#999999">{</span>
Intent intent <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Intent<span style="color:#999999">(</span>context<span style="color:#999999">,</span> SecondActivity<span style="color:#999999">.</span><span style="color:#c678dd">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
intent<span style="color:#999999">.</span><span style="color:#61aeee">putExtra</span><span style="color:#999999">(</span><span style="color:#669900">"param1"</span><span style="color:#999999">,</span> data1<span style="color:#999999">)</span><span style="color:#999999">;</span>
intent<span style="color:#999999">.</span><span style="color:#61aeee">putExtra</span><span style="color:#999999">(</span><span style="color:#669900">"param2"</span><span style="color:#999999">,</span> data2<span style="color:#999999">)</span><span style="color:#999999">;</span>
context<span style="color:#999999">.</span><span style="color:#61aeee">startActivities</span><span style="color:#999999">(</span>intent<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
如果你想要啟動該活動,只需:
<span style="color:#000000"><code class="language-java">button1<span style="color:#999999">.</span><span style="color:#61aeee">setOnClickListener</span><span style="color:#999999">(</span><span style="color:#c678dd">new</span> View<span style="color:#999999">.</span>OnClickListener<span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#999999">@Override</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onClick</span><span style="color:#999999">(</span>View v<span style="color:#999999">)</span> <span style="color:#999999">{</span>
SecondActivity<span style="color:#999999">.</span><span style="color:#61aeee">actionStart</span><span style="color:#999999">(</span>FirstActivity<span style="color:#999999">.</span><span style="color:#c678dd">this</span><span style="color:#999999">,</span> <span style="color:#669900">"data1"</span><span style="color:#999999">,</span> <span style="color:#669900">"data2"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span></code></span>
相關文章
- Android基礎之Activity全解析Android
- Android Activity的基本理解Android
- Android知識點回顧之Activity基礎Android
- Android基礎之Activity 執行模式與回退棧Android模式
- Android 基礎 -- Activity 生命週期實踐總結Android
- Android基本控制元件和Activity的基本應用Android控制元件
- Android基礎第八天易忘部分(Activity)Android
- android基礎學習-android篇day17-Android Fragment(碎片)基本使用AndroidFragment
- Android之Activity基類封裝Android封裝
- android基礎學習-android篇day17-Activity的生命週期(轉)Android
- 應用程式基礎知識:activity和intent——Android開發祕籍IntentAndroid
- 活動(Activity)的基本操作
- Maven基礎:Maven環境搭建及基本使用(1)Maven
- Android Retrofit 2.5.0使用基礎詳解Android
- Android RxJava:基礎介紹與使用AndroidRxJava
- [Android基礎]WebView的簡單使用AndroidWebView
- Android ActivityAndroid
- Activity 的 "啟動流程"(基於 Android 9.0)Android
- Android基礎Android
- Android的基本控制元件和Activity的應用總結Android控制元件
- Android PopUpWindow基本使用Android
- Android基礎及應用 Service的使用Android
- Android之ActivityAndroid
- ThinkPHP 6.0 基礎教程 - 基本流程PHP
- Docker基礎與基本概念Docker
- [Android]使用ActivityGroup來切換Activity和LayoutAndroid
- Android基礎—FragmentAndroidFragment
- Android Studio1.4.x JNI開發基礎-基本環境配置Android
- 『與善仁』Appium基礎 — 24、等待activity出現APP
- Android ORM 框架:GreenDao 使用詳解(基礎篇)AndroidORM框架
- Android Volley 基本使用Android
- Android Button的基本使用Android
- android官方Api 理解Activity生命週期的回撥機制(適合有基礎的人看)AndroidAPI
- [java基礎]之基本錯誤分析Java
- shell基礎 — 基本正規表示式
- Pytorch基礎-張量基本操作PyTorch
- RabbitMq基礎教程之基本概念MQ
- WordPress基礎之基本SEO設定