In my app I have a logout functionality. If user clicks logout it goes to home screen. Now I am exiting my app by pressing back button. But what I want is I need to exit automatically(i.e Programmatically) as same like as back button functionality. I know by calling finish() will do the functionality. But the thing is it goes to the previous activity.
Step 3: Working with MainActivity.java file In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so. Otherwise, don't exit.
just use finish(); – V.J. @user1216003 you are on right way. you will do same as back button with setting the flag in intent.
onBackPressed()
is supported since: API Level 5
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { onBackPressed(); } } @Override public void onBackPressed() { //this is only needed if you have specific things //that you want to do when the user presses the back button. /* your specific things...*/ super.onBackPressed(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With