Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to press back button in android programmatically?

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.

like image 718
vinothp Avatar asked May 23 '12 11:05

vinothp


People also ask

How do you back press on Android?

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.

How do you press back button?

just use finish(); – V.J. @user1216003 you are on right way. you will do same as back button with setting the flag in intent.


1 Answers

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();    } 
like image 132
Tarun Avatar answered Sep 17 '22 03:09

Tarun