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?
Asked
Active
Viewed 579 times
0

Nikunj Patel
- 21,853
- 23
- 89
- 133
2 Answers
1
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);
}

Zappescu
- 1,429
- 2
- 12
- 25
-
i have six activity so , is this code for each activity or last activity – Nikunj Patel Jul 29 '11 at 08:35
-
This code will close only the activity in which you put it and come back to the activity that called it or, if the acticity is the first one, it will close the app. – Zappescu Jul 29 '11 at 09:45
1
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.

Community
- 1
- 1

Martin Foot
- 3,594
- 3
- 28
- 35