84

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
vinothp
  • 9,939
  • 19
  • 61
  • 103
  • Even if you press back button previous activity will be shown... – Tarun May 23 '12 at 11:17
  • 10
    just use finish(); – V.J. May 23 '12 at 11:20
  • 1
    @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); – vinothp May 23 '12 at 11:22
  • @user1216003 you are on right way. you will do same as back button with setting the flag in intent. – V.J. May 23 '12 at 11:24

8 Answers8

131

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();   
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Tarun
  • 13,727
  • 8
  • 42
  • 57
  • 3
    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 May 23 '12 at 11:21
  • 3
    @KKD if you have to support API below 5 then you have to use like above.. http://android-developers.blogspot.co.uk/2009/12/back-and-other-hard-keys-three-stories.html – Tarun May 23 '12 at 11:24
  • 1
    Then onBackPressed is good to go... Start your ExitIntent inside onBackPressed() – Tarun May 23 '12 at 11:29
  • 1
    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... – vinothp May 23 '12 at 11:37
  • I had problems with onBackPressed() on some tablets, as in crash. – Demonick May 23 '12 at 13:21
  • I have the following code in my second Activity: `getActionBar().setDisplayHomeAsUpEnabled(true);` How do invoke the "Back to main activity"? – Si8 Aug 07 '13 at 11:50
  • @SiKni8 you can call `getActivity().getFragmentManager().popBackStack();` – Tarun Aug 07 '13 at 12:16
45

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.

josephus
  • 8,284
  • 1
  • 37
  • 57
  • 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... – vinothp May 29 '12 at 12:17
  • 1
    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 May 06 '15 at 06:28
11

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.

Serj Moya
  • 149
  • 3
  • 10
5

Call onBackPressed after overriding it in your activity.

frogatto
  • 28,539
  • 11
  • 83
  • 129
drulabs
  • 3,071
  • 28
  • 35
  • 2
    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 – vinothp May 23 '12 at 11:19
  • 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. – drulabs May 23 '12 at 11:23
3

you can simply use onBackPressed();

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

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Abhishek Patil
  • 502
  • 1
  • 4
  • 12
0

Simply add finish(); in your first class' (login activity) onPause(); method. that's all

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
0

For Kotlin users and working with fragments:

use finish() if you are navigating through activities, but for fragments you can remove the fragment by its fragment tag.

When navigating to the fragment use:

myFragment = myFragment(this)            

parentFragmentManager.beginTransaction()
                .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                .add(R.id.baseFragmentContainer, myFragment, "MYFRAGMENT")
                .addToBackStack("myFragmentNameInStack")
                .commit()

then, when tapping your particular button and simulate a "back button" pressing, do the following:

    binding.someButton.setOnClickListener {
        activity?.supportFragmentManager?.
        popBackStack("myFragmentNameInStack", FragmentManager.POP_BACK_STACK_INCLUSIVE)
    }

This will remove the fragment added by tag "myFragmentNameInStack", showing on screen the previous one.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
0
public void onBackPressed() {
        Intent intent = new Intent(Intent.ACTION_MAIN);

        intent.addCategory(Intent.CATEGORY_HOME);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(intent);

    }

// make sure use in outside of onCreate Method

Wojuola Ayotola
  • 608
  • 8
  • 16