操作步骤如下:1、将启动页的android:launchMode设置成singleTask后,从启动页打开一个activity。2、按home键,将手机屏幕切到主界面。3、再点击启动页对应的程序图标。
4、程序会显示启动页,而没有显示它打开的activity。
安装文档中描述的launchMode设置成singleTask后,程序只会创建唯一一个启动页,并且所有由启动页打开的activity都会在这个Task中。这正是我想要的效果。但是,从程序从前台切到后台,再切回前台后,为什么它没有显示activity堆栈最顶端的activity呢,而是显示了root页面,也就是启动页呢?有没有办法可以让launchMode设置成singleTask后,仍然像设置成standard一样,在从后台切换回程序时,显示启动页打开的activity(栈顶的activity)啊?望大侠帮忙!!!

解决方案 »

  1.   

    "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task.
    这是文档中的说明,那么既然是只有一个启动页,你从程序从前台切到后台,再切回前台后,相当于还是要启动程序吧?
    既然你要启动程序是不是要启动这个唯一的启动页呢?
    所以就到了楼主所说的结果。
    第二,其实感觉也挺简单的,直接将android:launchMode="singleInstance"这样就应该可以了。
    测试过,能达到楼主要求。
      

  2.   


    谢谢你的热心帮助,O(∩_∩)O~不过还是有问题:
    1、“那么既然是只有一个启动页,你从程序从前台切到后台,再切回前台后,相当于还是要启动程序吧”。那为什么设置为“standard”的时候,能够显示栈顶的activity呢?2、设置成android:launchMode="singleInstance"后,这样操作:由启动页打开一个activity,然后按回退键。被打开的activity不见了,但是启动页也不见了,直接显示手机的主界面,应该显示启动页才合理啊。
      

  3.   

    谢谢你的热心帮助,O(∩_∩)O~不过还是有问题:
    1、“那么既然是只有一个启动页,你从程序从前台切到后台,再切回前台后,相当于还是要启动程序吧”。那为什么设置为“standard”的时候,能够显示栈顶的activity呢?2、设置成android:launchMode="singleInstance"后,这样操作:由启动页打开一个activity,然后按回退键。被打开的activity不见了,但是启动页也不见了,直接显示手机的主界面,应该显示启动页才合理啊。
      

  4.   

    1、“那么既然是只有一个启动页,你从程序从前台切到后台,再切回前台后,相当于还是要启动程序吧”。那为什么设置为“standard”的时候,能够显示栈顶的activity呢?
    设置为standard的时候,允许有多个启动项, 也就是说, 可以从任意一个activity中启动,具体的请看文档。
    2、设置成android:launchMode="singleInstance"后,这样操作:由启动页打开一个activity,然后按回退键。被打开的activity不见了,但是启动页也不见了,直接显示手机的主界面,应该显示启动页才合理啊。
    on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent. 
    文档中是这样说的,你设置成了singleInstance,. It's the only activity in the task。
    也说了解决的办法,就是你开启acitivty跳转的时候,添加flag  FLAG_ACTIVITY_NEW_TASK 即可。
      

  5.   

    <application android:icon="@drawable/icon" android:label="@string/app_name"  android:launchMode="singleTask">
    这样也可以实现楼主的效果,不过看需求。
      

  6.   

    楼主你好,有点疑问麻烦解答下,谢谢,
    1,启动页是之指应用lancuncher的第一个activity吗?
    2,如果是,我在test3中启动ChatView,然后按Home.找到刚才的应用再点击进去,出现的还是test3,而不是ChatView。
     <application android:icon="@drawable/icon" android:label="@string/app_name" >
            <activity android:name=".test3"
                      android:label="@string/app_name"
                      android:launchMode="singleInstance">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            
            <activity android:name=".ChatView"
             android:label="@string/app_name">
            </activity>
            
            
        </application>启动Activity的Intent.
    Intent mintent = new Intent();
    mintent.setClassName("test3.program", "test3.program.ChatView");
    mintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(mintent);3.如果我把应用设置为singaleTask就可以实现,启动的mintent也不需要设置FLAG_ACTIVITY_NEW_TASK标志。
      <application android:icon="@drawable/icon" android:label="@string/app_name" android:launchMode="singleTask">。
      

  7.   

    你所说的启动页是指 一个应用程序的第一个界面还是,还是显示所有应用程序图标的launch界面????
      

  8.   

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:launchMode="singleTask">
    这种方式是可以,不过如果仔细测试,你会发现有问题。
    我们有两个activity:A和B,启动程序的时候先显示A二秒(也就是通常的loading页面),然后启动B,并且关闭A自己
    如果通过Notification启动过程序B以后,按home键,再次打开程序,你会发现,它仍然是打开A,然后才再现B。
      

  9.   

    另:
    activity android:name=".test3"
      android:label="@string/app_name"
      android:launchMode="singleInstance">
    再加Intent.FLAG_ACTIVITY_NEW_TASK能实现每次都先启动A再启动B,且可以定位到之前打开的位置
    但,在A切换到B的过程中会出现黑屏!
      

  10.   

    黑屏确实存在,关键是我当前设置android:launchMode="singleInstance"的这个activity 的值不能带到上一个activity了,也就是说没有回调到onActivityResult这个方法哇?  还知道怎么回事儿么?
      

  11.   

    ...难道我的模拟器版本有问题么,,我试了半天,application里面没这属性,单个的设置又没看到从启动页启动,home,再进去是其他activity的效果
      

  12.   

    我也遇到了这个问题,查了sdk doc:
    As shown in the table above, standard is the default mode and is appropriate for most types of activities. SingleTop is also a common and useful launch mode for many types of activities. The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.若没有特殊要求,直接将launch mode设置为singleTop,即可实现lz需要的效果了。
     
    看了google group里的评论,深层次理解launch mode需要阅读
    launch modes, tasks, taskAffinities, and activity stacks.
      

  13.   

    android:launchMode="singleTask"能解决Activity切换是出现的黑屏嘛
      

  14.   

    第一个是singletask也就是启动第一个actvity的时候会放置放置到一个新的task下,当你再启动其他activity时第一个 activity就在栈的底部,当你再次进入程序时他会查看是否有其他任务的栈里含有这个singletask的activity如果它在底部那么就将在他上面的都finish掉,所以再回去总是第一个界面。From http://www.butno.net/3823;I think this is a reasonable analysis.
      

  15.   

    我也遇到这样的问题了,只是我的android:launchMode设置成singleInstance后,按home键,再次返回时这个界面就没有了,不知道该怎么办
      

  16.   

    这个问题其实很好解决的,你只要不把程序入口的Activity设置为singleTask就可以了。没必要把android:launchMode设置成singleInstance的