vinothp
vinothp

Reputation: 10059

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.

Upvotes: 86

Views: 138181

Answers (8)

dbs ray
dbs ray

Reputation: 1

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

Upvotes: 0

Abhishek Patil
Abhishek Patil

Reputation: 542

you can simply use onBackPressed();

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

Upvotes: 3

Sahil Kundaliya
Sahil Kundaliya

Reputation: 1

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

Upvotes: 0

Daniel Cettour
Daniel Cettour

Reputation: 304

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.

Upvotes: 1

Tarun
Tarun

Reputation: 13808

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

Upvotes: 131

drulabs
drulabs

Reputation: 3121

Call onBackPressed after overriding it in your activity.

Upvotes: 5

Serj Moya
Serj Moya

Reputation: 149

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.

Upvotes: 11

josephus
josephus

Reputation: 8304

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.

Upvotes: 46

Related Questions