Detect home button press in android

后端 未结 17 2178
抹茶落季
抹茶落季 2020-11-22 08:22

This has been driving me nuts for a while now.

Is there any way of reliably detecting if the home button has been pressed in an android application?

Failing

17条回答
  •  情深已故
    2020-11-22 09:10

    Try to create a counter for each screen. If the user touch HOME, then the counter will be zero.

    public void onStart() {
      super.onStart();
      counter++;
    }
    
    public void onStop() {
      super.onStop();
      counter--;    
      if (counter == 0) {
          // Do..
      }
    }
    

提交回复
热议问题