1

singleTask and singleInstance alone are not the answer.

I have activities in a stack ... n, n+1, n+2

elements in n+2 can open n+1

this creates stack

... n, n+1, n+2, n+1

I want it to create stack

... n, n+2, n+1 , where n+1 is moved to the top of stack

singleTask creates this stack

... n, n+1

and singleInstance creates this stack

n+1 where all previous activities are closed.

neither of which I want.

is there any launch mode I can use?

FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY only makes it so that this activity isn't loaded again from scratch, but doesn't clear the duplicate activity in history, maybe there is a combination of flags I can use? or I can possibly locate that activity in the index of the activity stack and remove it if it isn't the most recent activity ?

CQM
  • 42,592
  • 75
  • 224
  • 366

2 Answers2

2

I think you should use FLAG_ACTIVITY_REORDER_TO_FRONT.

If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running. For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.

Than
  • 2,759
  • 1
  • 20
  • 41
0

You should just add FLAG_ACTIVITY_REORDER_TO_FRONT to your intent. Without that flag, your scenario creates a stack as follow: (starting Activity A, A start B, and then B again start A)

TaskRecord{41ab0370 #23 A com.example.testproject U 0}
  Run #7: ActivityRecord{41c655a8 u0 com.example.testproject/.MainActivity}
  Run #6: ActivityRecord{41d378d0 u0 com.example.testproject/.SecondActivity}
  Run #5: ActivityRecord{41bf99b8 u0 com.example.testproject/.MainActivity}

But using the flag, without any specific launch mode, you will get the following stack, which I assume it's the one you need:

TaskRecord{41d8a838 #25 A com.example.testproject U 0}
  Run #6: ActivityRecord{41d89590 u0 com.example.testproject/.MainActivity}
  Run #5: ActivityRecord{41c52b80 u0 com.example.testproject/.SecondActivity}
m.hashemian
  • 1,786
  • 2
  • 15
  • 31