Search code examples
androidandroid-activityonresumeapplication-restart

Restart Activity with back button


Basically my MainActivity has a button which will become invisible after clicking and the SecondActivity will be called after a few seconds. However, when I press the back botton, the button on the MainActivity is still invisible. I want the MainActivity to restart/initialize. I knew it is something to do with onResume or onRestart but how can I implement these methods? Can anyone give me an example? Thanks.


Solution

  • You could call finish() on your MainActivity when you go to your second one. Then Override onBackPressed() in your SecondActivity and start the MainActivity again.

    @Override
    public void onBackPressed()
    {
        // create Intent and start MainActivity again
    }