Search code examples
androidback-buttononbackpressed

Find out where back button press is going


When user presses a back hardware button in my activity he will either be taken to a previous activity or to the home screen (depends on how he got into that activity in a first place). Is there a way to determine where exactly user will be taken inside of onBackPressed method?


Solution

  • In your home and previous activity when you start the intent to go to this activity, you can add an extra to the Intent.

    Intent.setExtra("PREVIOUS_ACTIVITY ", "HOME")
    

    Then you can check in your activity onBackPressed() if it came from the home or another activity.