Nikunj Patel
Nikunj Patel

Reputation: 22066

application still running

when i am test application to device it is still running after when i am close.
is is going totally close while i press button and button contain code of finish() but problem is rise when i am press back key of phone.
is any idea to solve this?

Upvotes: 0

Views: 585

Answers (2)

Martin Foot
Martin Foot

Reputation: 3664

You can listen to the back button key input and call finish() there.

See the answers here.

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

Are you sure you want your application to be closed when the back button is pressed though? This is different to the default android behaviour, where it will close your Application if the Activity hasn't been viewed for a while and the system's running low on memory. In general modifying default behaviour is not advised.

Upvotes: 1

Zappescu
Zappescu

Reputation: 1439

Applications in Android are automatically closed when in background (if you have not explicitally managed to stay awake even if in background). Anyway, for the back key problem, why not put the finish() in the back key call?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ( keyCode == KeyEvent.KEYCODE_BACK) {
        System.gc();    
        finish();       
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 1

Related Questions