Skip to content Skip to sidebar Skip to footer

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 d

Solution 1:

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 activitiesif (SomeClass.IsClosingFlow){
        if (isActivityInFlow()){
            this.finish();  
        }
        else{
            // If we got here, and we're not in the flow anymore
            SomeClass.IsClosingFlow = false;
        }
    }

Solution 2:

There's a bunch of different ways to manage this kind of application flow. One way that may work for you and is pretty easy is to just use startActivityForResult() when you call from one activity to the next. Then, when you are finally done, you just set the result and call finish(). In each activity you can then implement onActivityResult() so that it sets the result and calls finish() on itself. In this way you can chain forward a number of activities and then chain back to whoever started the chain.

Post a Comment for "How To Finish An Activity And Return To A Varied Location?"