androidoncreateonresumeandroid-homebutton

Which method is run when Home button pressed?


I have a Home replacement Activity from within which you can launch a number of apps. When you tap the Home button, you are returned to my Home replacement Activity.

As I understand, tapping the Home button creates an intent to launch the Home screen and then starts that intent (I might be wrong, please correct me if I am!). If this is the case, I'd expect the onCreate() method to be run whenever the Home screen is created. On the other hand, when you launch another activity, the Home screen invokes onPause(). So returning to it makes me assume onResume() is invoked.

If someone could just offer some enlightenment into this matter, the basic question is whether onResume() or onCreate() gets called when I tap the Home button, but additional details are welcome, I'm working on stuff that utilizes this heavily and want to know a lot about it.


Solution

  • tapping the Home button creates an intent to launch the Home screen and then starts that intent

    Correct.

    If this is the case, I'd expect the onCreate() method to be run whenever the Home screen is created

    Not necessarily. If it is already running, it would be called with onNewIntent().

    If someone could just offer some enlightenment into this matter, the basic question is whether onResume() or onCreate() gets called when I tap the Home button

    Any time any activity returns to the foreground from a user input standpoint, onResume() is called. Home screens should be no different in this regard.

    onCreate() is called when the activity is created. Existing activities are not created, but are merely brought back to the foreground. If what triggered the activity to return to the foreground was a startActivity() call, the activity will be called with onNewIntent() and onResume() (and usually onStart(), for that matter).