展会信息港展会大全

[Android Samples视频系列之ApiDemos] App-Activity-IntentActivityFlags
来源:互联网   发布日期:2016-01-14 09:31:44   浏览:1115次  

导读:1.前言该Demo的分析参考了mapdigit的关于Api Demos的文章,我在他的基础上更进一步分析了相关知识点。2.Demo效果3.Demo分析首先,进入到IntentActiv...

1.前言

该Demo的分析参考了mapdigit的关于Api Demos的文章,我在他的基础上更进一步分析了相关知识点。

2.Demo效果

3.Demo分析

首先,进入到IntentActivityFlags后的界面如下图所示:

通过布局文件可以知道该例子布局很简单:

[java]

<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的列表界面。

为什么两个按钮点击后实现的功能一样呢,它们在点击后的响应事件处理中有什么不同吗?我们先从一个主要的函数代码来看:

[java]

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]是通过

[java]

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)。

[java]

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

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港