How to catch event of coming back to an activity after hitting back

≯℡__Kan透↙ 提交于 2019-12-10 03:56:23

问题


I have a main screen for my application which then leads to different screens, from each of those hitting back takes you back to the main screen. I want to do some stuff every time a user is "coming back" to the main screen, How do I catch this kind of event???


回答1:


Use onResume() method in your main activity or Use startActivityForResult method in your activity by overriding the keyDown method in sub activities,it may help you




回答2:


you cand try this:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       if ((keyCode == KeyEvent.KEYCODE_BACK)) {
       //your stuff goes here
       }
    return super.onKeyDown(keyCode, event);
    }



回答3:


You could do something like -

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
       // Do your stuff
       return true;
   }
   return super.onKeyDown(keyCode, event);
}


来源:https://stackoverflow.com/questions/6636039/how-to-catch-event-of-coming-back-to-an-activity-after-hitting-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!