SingleTask和SingleInstance详解

来源:互联网 发布:java private和protect 编辑:程序博客网 时间:2024/04/29 19:57

           SingleTask(FLAG_ACTIVITY_NEW_TASK):  

          

            

        如图,有两个Application A 和 B。其中B中的activity b为SingleTask。首先启动B,在activity a中调用b,b又调用c。然后按Home键返回桌面。打开APP A。在Activity x中调用b。这时b的taskid和a的一样。说明系统没用重新创建Task和b的实例,而是直接调用TaskB中已经存在的b。但是TaskB中的activity c已经被Destroy掉。只有b和a了。b在栈顶。

       The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.


         SingleInstance:


        如图,B中的activity b为SingleInstance模式。打开B程序,在a中调用b,此时b的taskId和a的不一样,说明系统为b创建了一个新的task并实例化b。在b中调用c时,c的taskId和a一致。(当在c中依次按下返回键时,先返回到a,然后再返回到b,最后返回到桌面。)然后当按下Home键,打开A程序,然后再x中调用b时,系统不会再去创建b的实例,而是使用已经存在的b的实例。

         Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

        singleInstance模式只会创建一个Task,并且这个Task中只存在一个Activity。

原创粉丝点击