Search code examples
androidback-stack

How to finish an activity and return to a varied location?


My application implements an action which stretch upon 3-4 activities. the entry point to this process can be from different activities in the application. (HomeActivity, various displayActivities). I want to return to the starting activity once the last activity is finished successfully.

Is there a best practice way to do it? thankyou.


Solution

  • You can use a global static boolean to help you with this (in the example SomeClass.IsClosingFlow), Plus, you should define each activity to "mark" if it's in the "flow" (flow=meaning it's part of the pack of activities that need to be closed). I recommend using this mark as an abstract method if you all your activities are extending some abstract-activity (i.e. isActivityInFlow()).

    The following code demonstrates this, It needs to be places in onResume() of each activity in the application:

        // Check to see if we are in the process of closing activities
        if (SomeClass.IsClosingFlow){
            if (isActivityInFlow()){
                this.finish();  
            }
            else{
                // If we got here, and we're not in the flow anymore
                SomeClass.IsClosingFlow = false;
            }
        }