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;  }  

通过函数中的注释我们可以知道,这个函数的作用是通过启动一个IntentsArray来重建一个“返回栈”,本例中的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

更多相关文章

  1. Android中Tab类型主界面大总结一共5种方式
  2. PreferenceActivity 全接触
  3. Android(安卓)GridView 的使用
  4. Android(安卓)studio安装与调试
  5. hello,Android
  6. Android(安卓)Button按钮点击变换字体颜色
  7. Activity跳转动画
  8. Android(安卓)在APP存活或者被杀死情况下,点击通知栏打开指定Acti
  9. 新浪微博布局学习——妙用TabHost

随机推荐

  1. AndEngine绘制滚动文本TickerText
  2. Android 中Fragment实现(芝麻虽小,必不可
  3. ANDROID获取Task及Process
  4. android之android.os.NetworkOnMainThrea
  5. opencv for android 教程(环境搭建篇)
  6. Android 保存图片到SQLite
  7. Android中自带的RSA加密算法和JAVA中的区
  8. Android编程学习之抽象类AbsListView用法
  9. android 如何得到手机的信息..
  10. Android 中注解的使用