您的位置:首页 > 移动开发

Android ApiDemos详解之App_Activity_IntentActivityFlags(7)

2012-01-03 21:03 309 查看
首先,进入到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);
}
}


pi.send()就是实现了在满足条件的时刻触发PendingIntent中的Intent[]。
本期到此结束
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: