Search code examples
androidmultiple-instancesactivity-stack

Android remove multiple instances of same activity except the first


I saw many threads similar to this. But my scenario is different. This is my Activity flow

Activity A -> Activity A (optional)('n' times) -> Activity B -> Activity C

When I click a button in Activity C, I have to go back to First Activity A closing all others on top of it and perform a task there. When I click back button this flow should not be disturbed. I cannot use any android:launchmode except standard for my scenario. How can I achieve this?


Solution

  • Maybe you can create a boolean instance variable which stores whether an instance of A is first or not. Than in your onActivityResult method:

    protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
             if (resultCode == PREVIOUS_CLOSED && !mFirst) {
                 setResult(PREVIOUS_CLOSED);
                 finish();                
             }
    
    }