In a interview someone asked me this question. Suppose there are 4 activities in application. He wants back button to work normally on first three activities(i.e. going to previous screen on back button press); except the last.
He wanted that, when he press back button on fourth activity(screen), user should go to Home Activity(Starting Screen or first screen). But the contents on Home Activity should be same when user went from first screen to second screen. So I think, I can’t use Intent, as it will create new instance of Starting Activity.
The interviewer should be interviewing for his or her own replacement, if (s)he thinks that hacking the BACK button this way is a good idea.
Add
FLAG_ACTIVITY_REORDER_TO_FRONTor the combination ofFLAG_ACTIVITY_CLEAR_TOPandFLAG_ACTIVITY_SINGLE_TOPto theIntentused withstartActivity()to bring up your “home activity”. Either will cause the existing “home activity” instance to come to the foreground. If you want all other activities to be destroyed, use the second approach (FLAG_ACTIVITY_CLEAR_TOPandFLAG_ACTIVITY_SINGLE_TOP).