Android - How to catch that the Home button was pressed?

前端 未结 6 1609
无人共我
无人共我 2020-12-12 00:30

I am trying to catch when the user has pressed the Home button. I thought I could use

protected void onResume()
{
   registerReceiver(homeReceiver, new Inte         


        
6条回答
  •  萌比男神i
    2020-12-12 00:50

    An easiest way is to put a boolean variable at false when you exit from that activity to go in some other activity. Then put the variable at true in the onResume method. Check in the method onStop() if it is not false, so if it is true then the Home button is pressed.

    Something like:

    boolean exit=false;
    

    ...

    //wherever other activity starting
    exit=false;
    startActivity(activity);
    

    ...

    @Override
    protected void onResume() {
        super.onResume();
        exit=true;
    }
    

    ...

    @Override
    protected void onStop() {
        super.onStop();
        if(exit){
            //Home button is pressed
        }
    }
    

提交回复
热议问题