How to press back button in android programmatically?

125,727

Solution 1

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();   
}

Solution 2

You don't need to override onBackPressed() - it's already defined as the action that your activity will do by default when the user pressed the back button. So just call onBackPressed() whenever you want to "programatically press" the back button.

That would only result to finish() being called, though ;)

I think you're confused with what the back button does. By default, it's just a call to finish(), so it just exits the current activity. If you have something behind that activity, that screen will show.

What you can do is when launching your activity from the Login, add a CLEAR_TOP flag so the login activity won't be there when you exit yours.

Solution 3

Sometimes is useful to override method onBackPressed() because in case you work with fragments and you're changing between them if you push backbutton they return to the previous fragment.

Solution 4

Call onBackPressed after overriding it in your activity.

Solution 5

you can simply use onBackPressed();

or if you are using fragment you can use getActivity().onBackPressed()

Share:
125,727

Related videos on Youtube

vinothp
Author by

vinothp

Do what you like. Believe in what you do. Young and energetic individual with great passion on Mobile Application Development. Currently working as a full time mobile application developer for Android and iOS. In free time working on personal projects as well. My First Personal App - Location Plotter/ House Viewing

Updated on July 05, 2022

Comments

  • vinothp
    vinothp almost 2 years

    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.

    • Tarun
      Tarun about 12 years
      Even if you press back button previous activity will be shown...
    • V.J.
      V.J. about 12 years
      just use finish();
    • vinothp
      vinothp about 12 years
      @Tarun I am using this code to clear all the history ExitActivity.this.finish(); Intent intent1 = new Intent(ExitActivity.this,PinActivity.class); intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent1);
    • V.J.
      V.J. about 12 years
      @user1216003 you are on right way. you will do same as back button with setting the flag in intent.
  • vinothp
    vinothp about 12 years
    Thanks for you comment.. OnBackPressed i can insert code when back button is pressed.. What i want is i want to call the back button automatically... am i clear
  • drulabs
    drulabs about 12 years
    What is the point of this? you are writing what android does by default. It calls onBackPressed() when back button is pressed.... am I right?
  • drulabs
    drulabs about 12 years
    do one thing... wherever you want to call, whenever you want to call just call this function. you can call it inside onCreate based on a flag or any event you want.
  • Tarun
    Tarun about 12 years
    @KKD if you have to support API below 5 then you have to use like above.. android-developers.blogspot.co.uk/2009/12/…
  • Tarun
    Tarun about 12 years
    Then onBackPressed is good to go... Start your ExitIntent inside onBackPressed()
  • vinothp
    vinothp about 12 years
    Thanks for your comment i am not using back button.. I am using a logout button.. when user clicks it populate dialog asking yes or no.. if yes it goes to the home screen where i have to exit automatically but i stuck there... This is need because i am using alarm functionality in my app.. when ever the alarm popup the message the home screen coming background of it.. So i need to remove it thats what i am trying...
  • Demonick
    Demonick about 12 years
    I had problems with onBackPressed() on some tablets, as in crash.
  • vinothp
    vinothp about 12 years
    Hi, Thanks for your answer.. It sounds good, but i tried your approach again end up in the same login screen.. Could you help me out from this...
  • Si8
    Si8 almost 11 years
    I have the following code in my second Activity: getActionBar().setDisplayHomeAsUpEnabled(true); How do invoke the "Back to main activity"?
  • Tarun
    Tarun almost 11 years
    @SiKni8 you can call getActivity().getFragmentManager().popBackStack();
  • Yevgeny Simkin
    Yevgeny Simkin about 9 years
    This is the correct answer, but you should edit it because it's not always "just a call to finish()". In my case (and it works exactly as I intend) it's a way to move backwards through the fragment stack, and doesn't call finish() until you've gotten to the very first item on that stack.
  • Yevgeny Simkin
    Yevgeny Simkin about 9 years
    This is precisely correct and deserves more upvotes.