展会信息港展会大全

[Android面试题-3] Activity的四种加载模式
来源:互联网   发布日期:2016-01-14 10:39:46   浏览:2139次  

导读:题目:请简单介绍一些Activity的四种加载模式。分析:四种加载模式分别为standard, singleTop,singleTask,singleInstance,设置的位置在And...

题目:请简单介绍一些Activity的四种加载模式。

分析:四种加载模式分别为standard, singleTop,singleTask,singleInstance,设置的位置在Androidmanifest.xml中Activity元素的Android:launchMode属性。

1.standard:执行如下代码

[java] view plaincopy

Intent intent = new Intent();

intent.setClass(ActA.this, ActA.class);

startActivity(intent);

在栈中会产生一个新的Activity实例。

2.singleTop:同样执行上述代码,系统会检测到栈顶已经存在满足需要的Activity实例,所以将intent发送给栈顶的实例,而不会产生一个新的Activity实例。但是如果执行如下代码的话则会产生一个新的Activity实例,因为栈顶不存在满足需要的实例。

[java] view plaincopy

Intent intent = new Intent();

intent.setClass(ActA.this, ActB.class);

startActivity(intent);

3.singleTask:和singleTop不同的是,每次发送intent的时候,系统会检测整个栈来寻找满足需要的Activity实例,如果存在,将intent发送给它,不存在则产生新的。

4.singleInstance:singleTask相当于是在一个应用中某一类的Activity只存在一个实例,大家共享这个实例;singleInstance相当于是多个应用共享一个Activity的实例。例如,一个导游的应用中调用了地图应用,你在导游应用中打开地图界面定位到中关村,然后按Home键将导游应用放到后台,然后再打开手机上的百度地图,会发现百度地图此时正好被定为到了中关村,这说明此导游应用和百度地图共享了同一个Activity的实例。

下面是官方文档中的介绍,有兴趣的读者可以查阅:

standard:The default mode, which will usually create a new instance of the activity when it is started, though this behavior may change with the introduction of other options such as Intent.FLAG_ACTIVITY_NEW_TASK.

singleTop:If, when starting the activity, there is already an instance of the same activity class in the foreground that is interacting with the user, then re-use that instance. This existing instance will receive a call toActivity.onNewIntent() with the new Intent that is being started.

singleTask:If, when starting the activity, there is already a task running that starts with this activity, then instead of starting a new instance the current task is brought to the front. The existing instance will receive a call toActivity.onNewIntent() with the new Intent that is being started, and with the Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set. This is a superset of the singleTop mode, where if there is already an instance of the activity being started at the top of the stack, it will receive the Intent as described there (without the FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set). See the Tasks and Back Stack document for more details about tasks.

singleInstance:Only allow one instance of this activity to ever be running. This activity gets a unique task with only itself running in it; if it is ever launched again with the same Intent, then that task will be brought forward and its Activity.onNewIntent() method called. If this activity tries to start a new activity, that new activity will be launched in a separate task. See the Tasks and Back Stack document for more details about tasks.

由于笔者水平有限,给各面试题提供的思路或代码难免会有错误,还请读者批评指正。另外,热忱欢迎读者能够提供更多、更好的面试题,本人将感激不荆如有任何意见或建议,欢迎在评论中告知。

博主徐方磊对本博客文章享有版权。网络转载请注明出处http://blog.csdn.net/shishengshi。整理出版物请和作者联系。

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

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