Android中launcherMode="singleTask"详解<解决home键返回不能出现原界面问题>
扫描二维码
随时随地手机看文章
android中launcherMode有4中属性:standard(默认), singleTop,singleTask和 singleInstance;网上有好多例子讲解这四种关系的:下面我列举几个链接:
大明原创---->转载请标明出处:http://blog.csdn.net/wdaming1986/article/details/7304191
http://www.cnblogs.com/xiaoQLu/archive/2011/09/29/2195742.html
http://marshal.easymorse.com/archives/2950
http://blog.csdn.net/infsafe/article/details/5666964
http://www.j2megame.com/html/xwzx/ty/2027.html
等等。我不一一列举;
下面是我对singleTask的一点拙见,希望分享一下给大家:
最近研究android浏览器browser,这个BrowserActivity的launcherMode="singleTask",因为browser不断地启动自己,所以要求这个栈中保持只能有一个自己的实例,就像别人总结的这样“注意:singleTask模式的Activity不管是位于栈顶还是栈底,再次运行这个Activity时,都会destory掉它上面的Activity来保证整个栈中只有一个自己,切记切记”这点是毋庸置疑的。
问题:browser的launcherMode=“singleTask”,browser上网的时候,遇到播放视频的链接,就会通过隐式intent方式跳转找Gallery3D中的MovieView这个类来播放视频,这时候如果你点击home键,再点击browser,你会发现MovieView这个类已经销毁不存在了,而不会像保存这个MovieView的类对象,给客户带来的用户体验特别的不好。用户还得从新下载刚才的视频。不支持断点播放。为了验证这一问题,我专门写了个小例子来说明activity之间的生命周期:
(1)先建一个App1的工程。包名为:com.cn.daming.app1,包里面有一个类:App1Activity。里面的代码如下:
[java] view plaincopyprint? package com.cn.daming.app1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class App1Activity extends Activity { private Button mButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.v("daming", "App1Activity--11-->onCreate()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); mButton = (Button)findViewById(R.id.button1); mButton.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { Intent intent = new Intent(); intent.setClassName("com.cn.daing.app2", "com.cn.daing.app2.App2Activity"); startActivity(intent); } }); } @Override protected void onDestroy() { super.onDestroy(); Log.v("daming", "App1Activity--11-->onDestroy()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onPause() { super.onPause(); Log.v("daming", "App1Activity--11-->onPause()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onRestart() { super.onRestart(); Log.v("daming", "App1Activity--11-->onRestart()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onResume() { super.onResume(); Log.v("daming", "App1Activity--11-->onResume()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onStart() { super.onStart(); Log.v("daming", "App1Activity--11-->onStart()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onStop() { super.onStop(); Log.v("daming", "App1Activity--11-->onStop()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Log.v("daming", "App1Activity--11-->onNewIntent()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.v("daming", "App1Activity--11-->onSaveInstanceState()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } }
manifest的属性为:android:launchMode="singleTask" android:alwaysRetainTaskState="true"
(2)在建一个App2的工程,包名:com.cn.daing.app2 ;类名为:App2Activity;代码为:
[java] view plaincopyprint? package com.cn.daing.app2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class App2Activity extends Activity { private Button mButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.v("daming", "App2Activity--22-->onCreate()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); mButton = (Button)findViewById(R.id.button1); mButton.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { Intent intent = new Intent(); intent.setClassName("com.cn.daming.app1", "com.cn.daming.app1.App1Activity"); startActivity(intent); } }); } @Override protected void onDestroy() { super.onDestroy(); Log.v("daming", "App2Activity--22-->onDestroy()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Log.v("daming", "App2Activity--22-->onNewIntent()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onPause() { super.onPause(); Log.v("daming", "App2Activity--22-->onPause()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onRestart() { super.onRestart(); Log.v("daming", "App2Activity--22-->onRestart()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onResume() { super.onResume(); Log.v("daming", "App2Activity--22-->onResume()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.v("daming", "App2Activity--22-->onSaveInstanceState()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onStart() { super.onStart(); Log.v("daming", "App2Activity--22-->onStart()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } @Override protected void onStop() { super.onStop(); Log.v("daming", "App2Activity--22-->onStop()...."+" /n getTaskId() == "+ getTaskId()+" isTaskRoot() == "+isTaskRoot()); } }
manifest的属性为默认值;
(3)然后启动App1---->点击调用第二个App2button按钮的log:
从log中可以看出:从App1到App2是在一个task栈中,值都是33,并且App1Activity是位于栈底。根节点的。点击按钮的时候,调用App1Activity的onSaveInstanceState()----->onPause()----->App2Activity的onCreate()----->onStart()---->onResume()---->App1Activity的onStop()方法。
(4)点击调用第一个App1---->button按钮;
看一下log:
还在一个栈中,数字变了因为我退出后从新操作的,把以前的log清空了,这时候发现了一个奇怪的现象,启动第一个App1的时候,App2从onStop()---->onDestroy()了,彻底销毁了;这个对象就不存在了。这也就验证了browser点击视频播放,点击home键,再点击browser,视频播放停止的现象的,android的singleTask就是这个设计的。挺独特的。
(5) 要想解决这一个问题,也是有办法的,我经过两天的钻研,终于找到了方法,现分享给大家:就是在App1中启动一个activity,这个activity的launcherMode=“standard”,然后由它来启动App1Activity,这样就避免了这一现象;
启动一个默认的activity:LoginApp1Activity代码:
[java] view plaincopyprint? package com.cn.daming.app1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class LoginApp1Activity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = (Intent)getIntent().clone(); intent.setClass(LoginApp1Activity.this, App1Activity.class); startActivity(intent); finish(); } }
manifest.xml中的代码:
[html] view plaincopyprint?