William L. William L. - 1 year ago 129
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
I press the back key

MainActivity < NewActivity

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

Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download