Don't close application when Back button is pressed

3 votes · 1 comment

This is quick solution. Best practice will be use Service.

raw ·
copy
· download
public class TestActivity extends Activity { //... // Working for all API levels @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { moveTaskToBack(true); } return super.onKeyDown(keyCode, event); } // Alternative variant for API 5 and higher @Override public void onBackPressed() { moveTaskToBack(true); } }
Add a comment

1 Comment

I am using Fragment, instead of going back to the first fragment, pressing back button close the application. Any help will be great. thank you.

Reply · Dec. 27, 2013, 11:14 p.m.