How to go back to 3rd previous Activity in android?
In my android application I search for a cafes first. When user select one cafe next activity will show more details about it. Then user can view the map of the cafe. When user click on the Done button I need to redirect user to the search result which was the first activity. So how can I 开发者_运维百科do this ? Calling finish() method will only close the current activity.
Thanks in advance !
you can use finish() in every activity as you proceed or you can start your first activity with this flag FLAG_ACTIVITY_CLEAR_TOP
and it will clear all the top activity of your first activity and it will bring your first activity on top.Use like this:
Intent intent = new Intent(this, yourFirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Launch that activity with thiss flag Intent.FLAG_ACTIVITY_CLEAR_TOP
Reference : Android documentation
//DOES NOT FINISH ALL ACTIVITY
Intent intent=new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
//FINISH ALL ACTIVITY WHICH IS IN STACK ABOVE TARGET ACTIVITY
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent.FLAG_ACTIVITY_CLEAR_TOP
clears everything but your first activity, this should work fairly well.
According to Android Application Developer Forum you should initiate this activity in the following manner:-
startActivity(intent);
In order that the user still can return to a previous activity B one should not use Intent.FLAG_ACTIVITY_CLEAR_TOP
Instead the activity B should use startActivityForResult(...C...) And the same activity B should check the result with onActivityResult() and finish if DONE was clicked in the activity C. But if just return is clicked in the activity C then the activity B would show normally.
精彩评论