I have been developing this application for quite a while now and came up with this bug in the app. Initially when the application loads for the first time, it starts from the home activity. Then the user interaction will navigate application to activity1 which uses fragment1. The fragment1 has a button. If the user clicks on that button,the activity1 calls finish() and loads activity2 by calling startActivity().Also when the user clicks on the hardware back button from the fargment1, the application returns to the Home activity.
So the real issue here is that, when I am at activity2 and finish() the activity the application will show the Home activity. But i want to completely close the application on pressing back from the activity2.
The following is the code which starts the activity2 form the fragment1:
Intent intent = new Intent(context, Activity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
((Activity)context).finish();
I get that the Home activity has never been finish()-ed.
So how do I destroy the activity1 and home activity on navigating to the activity2, and make the activity2 only remaining activity in the application?
Thanks in advance.
First approach: use start activity for result instead of start activity and finish it in onActivityResult.
Second apparoach: Register a local broadcast in each required activity and in its receiver, finish it. From activtity2 send this broadcast. This will finiah all those activities in stack having that broadcast registered. I have used this approach for Logout implementation in many apps and it works well.