Search code examples
androidcountdownandroid-activity

How to make an Android Activity not save its state?


I have an activity solely for a countdown that leads on to the execution of specified tasks. When I back out of that activity and press the button that leads back to it the countdown is already over, how do I make it start fresh every time it is opened?


Solution

  • If you want to close the countdown Activity when you execute the specific task, just call finish(). This will remove it from the history stack completely.

    If you want to leave it open, so that the user can press Back and return to the countdown, simply start the countdown timer in your onResume() method instead of onCreate(). That way whenever your activity becomes active the countdown timer will start from scratch.

    @Override
    public void onResume() {
        super.onResume();
    
        //Start the countdown - replace with your code
        resetTimer();
        startCount();
    }