Search code examples
androidback-stack

How to go back to the previously opened application while finishing an Activity in Android


I have a Alarm application. When the alarm triggers a activity will be invoked which inflates a layout. There is a "OK" button in this layout. When I click on that I want to go back to my previous application. i.e; if I was using chrome then it should go to chrome, if I was playing some game then it should go back there. Now what happens is it goes to the home page of my Alarm App.

Below is my code for "OK" button:

public void stoptimer(View v) {
        if (null != player) {
            player.stop();
            player.release();
        }
        if (!emailStr.isEmpty()) {
            shareTimeEmail(this, emailStr);
        }
        this.finish();
    }

Solution

  • Call moveTaskToBack(true) instead (or both) of finishing the Activity.