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

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

1.standard:执行如下代码

Intent intent = new Intent(); intent.setClass(ActA.this, ActA.class); startActivity(intent); 
在栈中会产生一个新的Activity实例。

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

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 itis started, though this behavior may change with the introduction of other optionssuch asIntent.FLAG_ACTIVITY_NEW_TASK.


singleTop:If, when starting the activity, there is already an instance of the same activity classin the foreground that is interacting with the user, then re-use that instance. Thisexisting instance will receive a call toActivity.onNewIntent()with the newIntent that is being started.


singleTask:If, when starting the activity, there is already a task running that starts with thisactivity, then instead of starting a new instance the current task is brought tothe front. The existing instance will receive a call toActivity.onNewIntent()with the new Intent that is being started, and with theIntent.FLAG_ACTIVITY_BROUGHT_TO_FRONTflag set. This is a supersetof the singleTop mode, where if there is already an instance of the activitybeing started at the top of the stack, it will receive the Intent as describedthere (without the FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set). SeetheTasks and Back Stackdocument for more details about tasks.


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


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

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


更多相关文章

  1. Android(安卓)短信转换成彩信的消息数量(实例代码)
  2. Android(安卓)AlertDialog ————普通对话框
  3. 开源项目之Android-GL(OpenGL 编程)
  4. Android(安卓)ViewPager实例代码介绍2。
  5. Android(安卓)加载本地图片 和 服务器图片
  6. Android中Intent的几种使用方法
  7. Android中设计模式无处不在之单例模式
  8. 分析(Android下的任务和Activity栈)
  9. android volley 调用webService

随机推荐

  1. Android(安卓)应用开发笔记 - 切换图片(Im
  2. WebView深究之Android是如何实现webview
  3. 如何设计一款AndroidAPP
  4. android每日一问【2011-09-19】
  5. Android(安卓)ListView长按弹出对话框
  6. Android(安卓)混淆配置文件及常用第三方
  7. [android]android自动化测试十二之代码控
  8. 运行时权限解析以及申请的实现(可完美解决
  9. android 获取外置sd卡根目录
  10. Android 的res/values/colors自定义颜色