[Android Samples視頻系列之ApiDemos] App-Activity-IntentActivityFlags

來源:互聯網
上載者:User

1.前言

該Demo的分析參考了mapdigit的關於Api Demos的文章,我在他的基礎上更進一步分析了相關知識點。

2.Demo效果

3.Demo分析

首先,進入到IntentActivityFlags後的介面如所示:

通過布局檔案可以知道該例子布局很簡單:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"         android:orientation="vertical"android:padding="4dip"         android:gravity="center_horizontal"         android:layout_width="match_parent"android:layout_height="match_parent">      <TextView         android:layout_width="match_parent"android:layout_height="wrap_content"         android:layout_weight="0"         android:paddingBottom="4dip"         android:text="@string/intent_activity_flags"/>      <Button android:id="@+id/flag_activity_clear_task"         android:layout_width="wrap_content"android:layout_height="wrap_content"         android:text="@string/flag_activity_clear_task">          <requestFocus />      </Button>      <Button android:id="@+id/flag_activity_clear_task_pi"         android:layout_width="wrap_content"android:layout_height="wrap_content"         android:text="@string/flag_activity_clear_task_pi">          <requestFocus />      </Button>  </LinearLayout>  

整個版面配置容器是線性布局,各個元素在其中以垂直,水平置中的方式進行排布。

功能為:

點擊第一個”FLAG_ACTIVITY_CLEAR_TASK”的按鈕,程式介面會跳轉到Views->Lists樣本的介面,如。

點擊第二個”FLAG_ACTIVITY_CLEAR_TASK(PI)”的按鈕,程式介面依然會跳轉到Views->Lists樣本的介面,同。

如果大家多多觀察會發現,在我們點擊兩個按鈕中任意一個後,都會跳轉到ApiDemos->Views->Lists的樣本中,然後點擊“返回”會回到ApiDemos->Views,再點擊“返回”會回到最初進入ApiDemos的列表介面。

為什麼兩個按鈕點擊後實現的功能一樣呢,它們在點擊後的響應事件處理中有什麼不同嗎?我們先從一個主要的函數代碼來看:

private Intent[] buildIntentsToViewsLists() {          // We are going to rebuild our task with a new back stack.  This will          // be done by launching an array of Intents, representing the new          // back stack to be created, with the first entry holding the root          // and requesting to reset the back stack.          Intent[] intents = new Intent[3];             // First: root activity of ApiDemos.          // This is a convenient way to make the proper Intent to launch and          // reset an application's task.          intents[0] = Intent.makeRestartActivityTask(new ComponentName(this,                 com.example.android.apis.ApiDemos.class));             Intent intent = new Intent(Intent.ACTION_MAIN);          intent.setClass(IntentActivityFlags.this,com.example.android.apis.ApiDemos.class);          intent.putExtra("com.example.android.apis.Path", "Views");          intents[1] = intent;             intent = new Intent(Intent.ACTION_MAIN);          intent.setClass(IntentActivityFlags.this,com.example.android.apis.ApiDemos.class);          intent.putExtra("com.example.android.apis.Path", "Views/Lists");             intents[2] = intent;          return intents;  }  

通過函數中的注釋我們可以知道,這個函數的作用是通過啟動一個Intents Array來重建一個“返回棧”,本例中的Intent[]有三個元素。

Intent[0]是通過

makeRestartActivityTask(new ComponentName(this,                 com.example.android.apis.ApiDemos.class));  

方法得到的,通過官方文檔可以知道該方法返回一個Intent,用以重建一個Activity Task並保持為根(root)狀態。也就是說,com.example.android.apis.ApiDemos.class這個Activity在啟動後將會作為這一個Activity Task的入口Activity。

      Intent[1]和Intent[2]均是通過setClass()來指明具體的意圖,並向分別向各自鍵為:"com.example.android.apis.Path"的變數中存入"Views "和"Views/Lists"兩個值,用以指示ApiDemos擷取相應的內容來填充其List列表。

      主要函數看完了再分別看看兩個按鈕的點擊事件的實現:

      第一個按鈕“FLAG_ACTIVITY_CLEAR_TASK”點擊後執行如下代碼: startActivities(buildIntentsToViewsLists());

我們經常用到的是startActivity(Intent),而startActivities(Intent[])的作用與之完全相同,無非就是將Intent[]中的三個Intent所指向的跳轉目標Activity從後至前依次添加到當前Activity棧中,如果大家在點擊完按鈕後用“Back”鍵返回,會發現返回的順序和Intent[]中的順序從後至前一致。

      第二個按鈕“FLAG_ACTIVITY_CLEAR_TASK(PI)”作用和第一個按鈕實現的功能相同,只是運用了PendingIntent,它的功能實際上是事先定義好一個Intent,然後在滿足條件的時候啟動(即使當前Context銷毀,外部也可以從初始化PendingIntent中的Context啟動這一Intent)。

try {                  pi.send();              } catch (CanceledException e) {                  Log.w("IntentActivityFlags", "Failed sending PendingIntent", e);              }          }  

 

4.視頻講解:http://www.eyeandroid.com/thread-9802-1-1.html

 

5.這個Demo我們學會了:

 

1. Android Intent和PendingIntent的區別詳細分析

http://www.eyeandroid.com/thread-1419-1-1.html

2. PendingIntent的重複問題,當建立的PendingIntent匹配上一個已經存在的PendingIntent時可能不會建立新的

http://www.eyeandroid.com/thread-9784-1-1.html

3. Reference:PendingIntent翻譯

http://www.eyeandroid.com/thread-9785-1-1.html

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.