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

蓝咒 提交于 2019-12-11 03:45:37

问题


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.


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



回答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.



来源:https://stackoverflow.com/questions/14499034/how-to-finish-an-activity-and-return-to-a-varied-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!