Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control hardware back button [closed]

Tags:

android

In my application, I want to control the hardware back button. That means in my app there are four or five activity. Now suppose I move from one activity to another.

Now in my case when I press the hardware back button I want my app to exit from any activity where I have been but in my app when the press this back button than it doesn't exit my app until it reaches the first activity... so my question is that what should I do so that when I press the hardware back button it will exit my app... please suggest me.

like image 515
AndroidDev Avatar asked Jun 27 '11 05:06

AndroidDev


People also ask

What is hardware back button in Android?

The hardware back button is found on most Android devices. In native applications it can be used to close modals, navigate to the previous view, exit an app, and more. By default in Ionic, when the back button is pressed, the current view will be popped off the navigation stack, and the previous view will be displayed.

Which callback of modal is fired when hardware back button is pressed?

The Backhandler API detects hardware button presses for back navigation, lets you register event listeners for the system's back action, and lets you control how your application responds. It is Android-only.


1 Answers

Try this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return true;
    }
return false;
}
like image 115
Patel Ekta Avatar answered Nov 15 '22 04:11

Patel Ekta