00 Votes

Android: Catch Back Button

Question by Guest | 2013-10-05 at 10:26

When programming an Android App, is it somehow possible to intercept the back button in order to switch between a custom event?

My Main Acitivity provides an overview and various things that you can display. Currently, in the case, the user presses the back button, the complete App is closed immediately. Instead, the application should only be closed when the main activity is displaying the overview. Moreover, when selected some of the items, the back button should cause to come back to the overview instead of closing the program. Only if you click again on the back button (while in the overview mode), the whole App should be closed.

Is this behavior somehow possible to implement? Another use for this would be, for example, a confirmation in the sense of "Do you really want to close the program?". Thank you.

ReplyPositiveNegative
0Best Answer0 Votes

Your intend can be implemented without further ado. You have only to override the event onBackPressed(), which is executed when the back button is clicked. Just copy the following lines into the code of your respective activity:

@Override
public void onBackPressed() {
    super.onBackPressed();
}

With this, the behavior of your app is like ever before. The super.onBackPressed() ensures that the default action of the back button is still running. But if you omit this line, nothing happens when clicking on the back button.

What you also need is a condition deciding which action should be executed by clicking the back button. For this, we are introducing the variable canClose.

private boolean canClose = true;

public void showDetails() {
   // Code to display the detailed view
   canClose = false;
}

public void showOverview() {
   // Code to display the overview
   canClose = true;
}

@Override
public void onBackPressed() {
    if (canClose) {
        super.onBackPressed();
    } else {
        showOverview();       
    }
}

We declare the canClose-variable directly in our activity and set it to true at the beginning. That means, when clicking the back button immediately after starting the app, this would close the application.

But, if we select some of the details after starting the app (showDetails), we temporarily set canClose to false. This has the effect, that with an open detail view, clicking the back button first shows the overview (showOverview). At the same time, we set canClose to ture, so that the next click on the back button actually closes the entire app.
2013-10-05 at 13:34

ReplyPositive Negative
Reply

Related Topics

Android Splash Screen Tutorial

Tutorial | 0 Comments

Android Getting Sound Levels

Open Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.