How to programmatically press the android button?

In my application, I have a logout function. If the user logs out, he goes to the main screen. Now I am leaving the application by clicking the back button. But I want me to automatically exit (that is, programmatically) in the same way as the back button functions. I know that calling finish () will perform this function. But the fact is that this refers to previous activities.

+74
android android-activity activity-finish
May 23 '12 at 11:14
source share
6 answers

onBackPressed() supported with: level 5 API

 @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(); } 
+115
May 23 '12 at 11:20
source share

You do not need to override onBackPressed() - it is already defined as the action that your activity will do by default when the user clicks the back button. So just call onBackPressed() whenever you want to press the back button programmatically.

This will only result in a finish() call, though;)

I think you're confused by what the back button does. By default, this is just a finish() call, so it just exits the current activity. If you have something behind this event, this screen will be displayed.

What you can do is start your activity from the input, add the CLEAR_TOP flag so that the input activity is not there when you exit it.

+38
May 23 '12 at 11:27
source share

It is sometimes useful to override the onBackPressed () method, because if you work with fragments and you change between them, if you click a button, they return to the previous fragment.

+8
Sep 03 '13 at 7:20
source share

Call onBackPressed after overriding it in your activity.

+5
May 23 '12 at 11:16
source share

You can just use onBackPressed ();

or if you use a fragment, you can use getActivity (). onBackPressed ()

+1
Jan 01 '19 at 8:22
source share

Just add finish (); in your first class' (login) onPause (); method. all this

0
Jan 13 '19 at 23:29
source share



All Articles