Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Avoiding an activity to destroy, just stopping or pausing it when pushing the back button

Tags:

android

I would like to pausing or putting the application on background when pressing the back button, I don't want the application to go through the destroy state. Things are when I override onKeyDown and when I force to pause or stop the application by using onPause, I have some issuees with the wakelock and application crash, but when I press home button I go through onPause method and I have no exception, it's weird!!

like image 467
Ghostwan Avatar asked Mar 19 '10 13:03

Ghostwan


People also ask

What happens to activity when back button is pressed?

Android maintains a back stack of destinations as the user navigates throughout your application. This usually allows Android to properly navigate to previous destinations when the Back button is pressed.

How do you pause and resume activity on Android?

Pause Your Activity You should usually use the onPause() callback to: Stop animations or other ongoing actions that could consume CPU. Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).

How do I skip back pressed activity on Android?

In Android we can change the default behaviour of the any activity like keypress, back button press, swap left swap right, to do Disable Back Press we need to override default functionality of onBackPressed() or onKeyDown() method in Activity class.

What is pause method in Android?

App pause is a feature in Android Q which allows us as users to pause a specific application for that day. Once an application is in pause state, you will not receive notifications from the app for the rest of the day.


2 Answers

Try to use the method onBackPressed(), like this:

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}
like image 160
Cristiano Santos Avatar answered Oct 04 '22 14:10

Cristiano Santos


The answer is

public boolean onKeyDown(int keyCode, KeyEvent event)
{

    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        moveTaskToBack(true);
        return true; // return
    }

    return false;
}
like image 45
Ghostwan Avatar answered Oct 04 '22 14:10

Ghostwan