William L. William L. - 5 months ago 10
Android Question

Check if back key was pressed in android?

Say I'm on my main activity and I start a new activity

MainActivity > NewActivity


And from
NewActivity
I press the back key

MainActivity < NewActivity


I want
MainActivity
to do something if it's being displayed after
NewActivity
is closed, but not when
MainActivity
is run normally, such as when first running the application. Does anyone know if this is possible?

Answer

A couple of ideas:

  • You can just set a flag in MainActivity when it fires up NewActivity.
  • You can call startActivityForResult from MainActivity and arrange for NewActivity to set a result, which you will receive in MainActivity.onActivityResult() when NewActivity finishes.
Comments