The pit encountered when the two activities repeatedly jump to each other

An application I have been working on recently, the main body is two activities, and it needs to jump back and forth between the two activities when interacting. First, Act A starts first, then Act A starts Act B, and then Act B can pass startActivity() (no other Method, right?) Go back to Act A again, and you can also enter Act B again in Act A; among them, we only need to initialize A and B once, that is, the onCreate() method is only executed once, and the existing Act A is reused later. , B instance.


  It seems like a very simple requirement, the first thing that comes to my mind is that the launchMode of both Activity is set to singleTask.

  In singleTask mode, there will only be one instance of the Activity in the Task stack . In standard mode, repeatedly using Intent to start Activity will create a new Activity instance, that is, onCreate() will be executed. At this time, there will be multiple identical Activity instances in Task, which obviously does not meet the requirements. Using singleTask mode seems to be able to Avoid this problem.

  The problem of repeated instances after using singleTask is solved. There is indeed only one instance of Act A and Act B in the Task stack at the same time, but through debugging, it is found that when the following jumps ( means start)

  Act A→ Act B → Act A

After the jump, the onDestroy() method of Act B is called, that is, Act B is destroyed.

  Act A→ Act B → Act A → Act B

, onCreate() is called when Act B is started for the second time . At this time, Act B is not Act B , it is a brand new instance.


  After carefully reviewing the relevant information, it is found that in a Task , the Activity instance is stored in the form of a stack, and the data structure of the stack is characterized by first-in, last-out. Usually an Application has a default Task . When the launchMode of the Activity is all singleTask, after the first Act A → Act B ,

Act B is at the top of the stack, and Act A is at the bottom of the stack; when Act A → Act B → Act A , since the Act A instance already exists, there is no need to create a new one, just transfer it to the top of the stack. The point is, the Activity above Act A Instances are removed until Act A is at the top of the stack. When we start Act B again , since there is no instance of Act B in the stack, an instance of Act B will be re-created and pushed onto the top of the stack.

  The cause of the problem is known, so how to solve it?  Since Acts A and B are stacked on the same stack, the problem of removing the top of the stack will occur. Can A and B be assigned to different Task stacks respectively? This is obviously feasible. In this case, Act A and B are in different stacks, and each stack has at most one instance. But later found out that this method is obviously overkill. I did this at the time:

  Specify android :taskAffinity= "daily.task.test" for Activity B in AndroidManifest.xml

Then add Flag in the Intent that Activity A starts Activity B :

     Intent.FLAG_ACTIVITY_NEW_TASK
At this point, there are two Tasks . After both Activities are started, the intuitive feeling is that an application has two cards in the background card list. like this:

Later, I carefully read a lot of information introduction, in this blog (feeling the collection and sorting of the great gods)
http://blog.csdn.net/lizhiguo0532/article/details/7480993
中注意到这个Flag:
    Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
表示“如果这个activity已经启动了,就不产生新的activity,而只是把这个activity实例加到栈顶“。这正是我们需要的
 
索性删除taskAffinity,LaunchMode都改为改为standard,启动时的Intent都添加上Flag:
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
问题完美解决

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324378451&siteId=291194637