Back button to close the application

I have a screen saver that leads to the main navigation screen, which has animations to enter buttons. I want me to close the application when the back button is pressed. It currently reloads activity (main) when the return button is called - why is this?

I looked at the forums, and one way was to use the finish () method. I tried to implement this in the main.java class as follows:

public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if( keyCode == KeyEvent.KEYCODE_BACK ) 
    {     
        this.finish();

        return true;
    }
    return false;
}

But it wasn’t higher - what am I doing wrong?

Greetings

UPDATE

Greets everyone for a quick response, but none of these works. But I think I know why - my class only implements the onCreate () method, and no one else. Maybe that's why all other methods fail?

UPDATE

. , , , :

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if ( keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ) 
    {
        // do something on back.
        moveTaskToBack( true );
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

, finish(), onBackPressed(), ekawas, ?

+3
4

onBackPressed, , keydown onKeyDown.

+1

, finish startActivity, onKeyDown:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
            finish();
        }

        return super.onKeyDown(keyCode, event);
    }
+1
@overide
public void onBackPressed() {
   finish();
   return;
}

, .

+1

All Articles